We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6726
    • 7,075 Posts
    Somebody posted a snippet (sorry, don’t remember who :/ ) called GetChildID, which I saved on my HDD but have been unable to find back on the forums (even with google...) :

    <?php
    $docid = (isset($docid)) ? $docid: $modx->documentIdentifier;
    
    $children= $modx->getActiveChildren($docid, 'id', 'ASC');
    foreach($children as $value){
     if($value['pagetitle']==$childsName){
        $childsID= $value['id'];
        return $childsID;    
        break;
     }
    }
    ?>


    but it doesn’t seem to work (nested or not).

    What I need is a snippet which does exactly the opposite of UltimateParent, e.g a way to use as a nested snippet to automate the startID of a Ditto or DropMenu query.

    This startID is two level *deeper* than the current document.

    Can anyone help me ?

    Thanks !
      .: COO - Commerce Guys - Community Driven Innovation :.


      MODx est l&#39;outil id
      • 10487 MODX Staff
      • 1,535 Posts
      Hi David, which version of MODx are you using for this?

      In 0.9.5, there is a MODx API function called getChildIds() that you could use (it was also in 0.9.2 but didn’t feature the depth parameter which would handy in your case) - if you can use that function, then it would also save hitting the DB as it uses the documentMap array that is in the cache. All you would need to do is wrap it in a snippet to use in other snippet calls.

        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 22303 MODX Staff
        • 10,725 Posts
        Something like this maybe? (in 0.9.5)
        <?php
        $docid = (isset($docid)) ? $docid: $modx->documentIdentifier;
        
        while ($children= $modx->getChildIds($docid, 1)) {
            $childID= reset($children);
            $docid= $childID;
        }
        return $docid;
        ?>
          • 6726
          • 7,075 Posts
          @Garry : Using 0.9.5, yes you’re right.

          Thanks Jason grin works fine !

          It seems it gets the ultimate child though... Could there be a way to set the depth parameter as Garryn suggest ? that would be just awesome !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: davidm at Oct 30, 2006, 04:06 PM

            It seems it gets the ultimate child though... Could there be a way to set the depth parameter as Garryn suggest ? that would be just awesome !

            <?php
            // docid param sets the document to start from (uses current document if not set)
            $docid = (isset($docid)) ? $docid: $modx->documentIdentifier;
            // depth param optionally sets the depth limit to get the child id from (default 100)
            $depth = (isset($depth) && intval($depth)) ? intval($depth) : 0;
            
            // set limit to an arbitrary maximum depth
            $limit= 100;
            if ($depth) {
                // if depth is set, set the limit to depth
                $limit= $depth;
            }
            // while above the depth limit and there are more children,
            //   get the docid of the child and decrement the limit
            while ($limit > 0 && $children= $modx->getChildIds($docid, 1)) {
                $childID= reset($children);
                $docid= $childID;
                $limit--;
            }
            return $docid;
            ?>
              • 6726
              • 7,075 Posts
              Thanks a lot for this Jason !

              I guess we now can add UltimateChild to the repository grin
                .: COO - Commerce Guys - Community Driven Innovation :.


                MODx est l&#39;outil id