We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12151
    • 115 Posts
    hi there, is there a way to know how many parents any given document has?
    or, how far down the tree structure the document is?

    if my tree structure is as follows:

    -level 1 page
    - level 2 page
    - level 3 page
    - level 4 page
    - level 5 page (current page)
    - level 3 page
    - level 2 page

    eg: my code needs to know that the selected page above is at level 5 (or has 4 parents). is this possible?

    thanks smiley

    EDIT: once again, solved my own problem. if anyone’s interested:

    $thisId = (isset($id)) ? $id: $modx->documentIdentifier;
    $arrDocParents[] = $thisId;
    while (($pid = $modx->getDocument($thisId,'parent')) && ($pid['parent'] != 0)){
      $thisId= $pid['parent']; 
      $arrDocParents[] = $thisId; 
    } 
    $countParents = count($arrDocParents);
    unset($arrDocParents);
    
    return $countParents;


      • 22303 MODX Staff
      • 10,725 Posts
      FYI, a much more efficient approach is to simply use the $modx->getParentIds() function...
      $noOfParents= count($modx->getParentIds($modx->documentIdentifier, 100));

      By default, it will only grab 10 parents, so I used a height parameter of 100 in this example to show how you can look for a larger number of parents.

      This method does not hit the database, whereas your function would issue a SQL query for each parent that existed.
        • 12151
        • 115 Posts
        genious! thank you smiley
          • 6726
          • 7,075 Posts
          Cool tip Jason, bookmarking this !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l'outil id
            • 38803
            • 3 Posts
            ctrl+D smiley