We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5340
    • 1,624 Posts
    Hi,

    On every project I find myself in need of the parentId of a parent. For example

    [[Wayfinder? &startId=`[~1234~]` &outerTpl=`LeftMenuOuterTpl` &rowTpl=`LeftMenuRowTpl` &hereClass=`selected`]]

    would look much better like

    [[Wayfinder? &startId=`[*parentparent*]` &outerTpl=`LeftMenuOuterTpl` &rowTpl=`LeftMenuRowTpl` &hereClass=`selected`]]

    I know there are diffrent ways to achieve the same functionality using a snippet but would be a nice touch to have something like [*parentparent*] or [*parentparentparent*].

    If you would like this functionality maybe we could ask for it in a future release. Shouldn’t be hard to implement.

    Thx
      • 26931
      • 2,314 Posts
      in case you mean [!Wayfinder?startId=`[[UltimateParent]]` !]

      it’s a MODx 0.9.6.3 built-in Snippet
        • 5340
        • 1,624 Posts
        It’s not about the ultimate parent.

        Eg:

        [[Wayfinder? &startId=`[*parentparent*]` &outerTpl=`LeftMenuOuterTpl` &rowTpl=`LeftMenuRowTpl` &hereClass=`selected`]]

        OR

        [[Wayfinder? &startId=`[[UltimateParent? &topLevel=`2`]]` &outerTpl=`LeftMenuOuterTpl` &rowTpl=`LeftMenuRowTpl` &hereClass=`selected`]]


        [*parentparent*] is more intuitive and it might be a nice touch.

          • 16183
          • 1,390 Posts
          Not sure what exactly are you trying to achieve? You can use [*parent*] if you wish. It is a modx doc variable. It will give you the id of the parent of the current document. UtlimateParent is even more dynamic and offers other benefits including the parentparent concept you intimate.

          cheers/k

            • 22303 MODX Staff
            • 10,725 Posts
            UltimateParent allows you to specify the top level of the parent you want to retrieve, so it essentially does exactly what you are looking for. Alternatively, using @INHERIT in a TV to specify the parent allows you to set the value at a specific level and allow all children to inherit that value, which you can use to achieve the same effect.
              • 5340
              • 1,624 Posts
              I know UltimateParent does what I need but for some reason it feels weird to use.

              Here’s the specs of new snippet based on UltimateParent. Let me know what you think. I’ll try to implement as soon as possible.


              * @name ParentParent
              * @version 0.1
              * @author Cipa
              *
              * @param &id The id of the document whose parent you want to find.
              * @param &extraLevels Adds extra levels to the search.
              *
              * @license Public Domain, use as you like.
              *
              * @example [[ParentParent]]
              * Will return the id of the parent of the parent for the current document
              *
              * @example [[ParentParent? id=`30`]]
              * Will return the id of the parent of the parent for document 30
              *
              * @example [[ParentParent? id=`30` &extraLevels=`1`]]
              * Will return the id of the parent of the parent of the parent for document 30
              *
              * This snippet travels up the document tree from a specified document and
              * returns the parent id of the parent. To add an extra level to the search use the &extraLevels param

              Thx
                • 5340
                • 1,624 Posts
                So here’s the snippet
                <?php
                /**
                 * @name ParentParent
                 * @version 0.1
                 * @author Cipa
                 * 
                 * @param &id The id of the document whose parent you want to find.
                 * @param &extraLevels Adds extra levels to the search.
                 * 
                 * @license Public Domain, use as you like.
                 * 
                 * @example [[ParentParent]] 
                 * Will return the id of the parent of the parent for the current document
                 *
                 * @example [[ParentParent? &id=`30`]] 
                 * Will return the id of the parent of the parent for document 30
                 *
                 * @example [[ParentParent? &id=`30` &extraLevels=`1`]] 
                 * Will return the id of the parent of the parent of the parent for document 30
                 * 
                 * This snippet travels up the document tree from a specified document and
                 * returns the parent id of the parent. To add an extra level to the search 
                 * use the &extraLevels param.
                 * To be used with a least 3 levels of documents. If less the parent id or the document id will be returned
                 * 
                 * Usage with wayfinder(note the [! !])
                 * [!Wayfinder? &startId=`[[ParentParent]]`!]
                 * [!Wayfinder? &startId=`[[ParentParent? &extraLevels=`1`]]`!]
                 *
                 * Inspired by UltimateParent
                 */
                
                $id = isset ($id) && intval($id) ? intval($id) : $modx->documentIdentifier;
                $extraLevels = isset ($extraLevels) && intval($extraLevels) ? $extraLevels : 0;
                $levels = 2 + $extraLevels; //the default start level is 2 which corresponds to the parent of the parent
                
                $parentsIds = array_values($modx->getParentIds($id)); //get parents and make numeric keys
                $parentsIdsNo = count($parentsIds);
                
                if(array_key_exists($levels-1, $parentsIds)){
                	return $parentsIds[$levels-1];
                }elseif($parentsIdsNo){
                	return array_pop($parentsIds);
                }
                
                return $id;
                
                ?>
                



                I have a dilemma though. When used with Wayfinder like [!Wayfinder? &startId=`[[ParentParent]]`!] there is no way to cache the Wayfinder call.
                Which is very bad in my opinion. Are there any ways I can "register" the [[ParentParent]] output into the modx object so I can do something like
                [[Wayfinder? &startId=`[*parentparent*]`]] where parentparent is a variable that is global set up by ParentParent snippet.

                One can call the 2 snippets sequentially
                [[ParentParent]] <- will set up a global parentparent variable
                [[Wayfinder? &startId=`[*parentparent*]`]] <- Wayfinder knows about parentparent

                Thx


                  • 5340
                  • 1,624 Posts
                  I think the placeholder works only in the snippet not outside it.

                  Here’s a solution that works well when caching a wayfinder call that uses another call to a snippet:
                  Create a snippet called ’customWayfinderCall’ and paste the code below

                  <?php
                  $parentparent = $modx->runSnippet("ParentParent");
                  $wayfinderOutput = $modx->runSnippet("Wayfinder", array('startId' => $parentparent));
                  
                  return $wayfinderOutput;
                  ?>
                  


                  use [[customWayfinderCall]] in your template





                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Any variables placed in the $modx->placeholders array using $modx->setPlaceholder($name, $value) will be available to all following snippets or anywhere in the template or content. The placeholders array is global to MODx. The snippet creating the placeholder has to appear in the page source before the Wayfinder snippet; otherwise the Wayfinder snippet is evaluated first, before the placeholder is set.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 5340
                      • 1,624 Posts
                      Hi Susan,

                      I did tryed that.

                      There was an extra line before the return in the ParaenParent snippet

                      $modx->setPlaceholder(’parentparent’, $id);

                      The call was done like:
                      [[ParentParent]]
                      [[Wayfinder? &startId=`[+parentparent+]`]]

                      But it didn’t work. Am I using the setPlaceholder function the wrong way?