We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13416
    • 6 Posts
    At the moment the ModX Class has a method getChildIds, but that method does not preserve the structure of the document tree, instead it just returns a plain list.

    I would suggest to add a parameter to getChildIds that determines if you get a flat version or the tree (or to add a new function getChildIdsAsTree). This could be just a simple associative array that mirrors the tree structure:
    ID->entry
    entry = {null | array(ID->entry)}.

    This would be usefull in all cases where you have to get the subtree to a document Id, for example if you want to build a menu.

    The modified function might look like this (the arrows indicate differences to the current version of getChildIds()):
        /**
         * Gets all of the child resource ids for a given resource.
         *
         * @param integer $id The resource id for the starting node.
         * @param integer $depth How many levels max to search for children (default 10).
    -->  * @param boolean $asTree If true an associative array is returned, if false 
         * @return array An array of all the child resource ids for the specified resource.
         */
        function getChildIds($id= null, $depth= 10, $asTree = false) {
            $children= array ();
            if ($id !== null && intval($depth) >= 1) {
                $id= intval($id);
    -->         if($asTree) {
    -->	        $children[$id]= array();
    -->         }
                if (isset ($this->resourceMap["{$id}"])) {
                    if ($children= $this->resourceMap["{$id}"]) {
                        foreach ($children as $child) {
                        	$processDepth = $depth - 1;
                            if ($c= $this->getChildIds($child, $processDepth)) {
    -->                         if($asTree) {
    -->                             $children[$id]= $c;
    -->                         } else {
    -->                             $children= array_merge($children, $c);
    -->                         }
                            }
                        }
                    }
                }
            }
            return $children;
        }
    


    I am not sure if this method saves the user any work, but since the tree structure is there in the beginning on might as well preserve it for later use.
      • 3749
      • 24,544 Posts
      Interesting idea, you might file that on Jira through the link at the top of this page as a feature request.

      Have you tried using the Wayfinder snippet for this? It will display the tree structure (or any part of it) in pretty much any way you specify. It will also send it’s entire output to a single paceholder if you want to process it before display or not display it at all.

      You could also look at the various site map snippets for code that preserves the tree.

      Ditto would also be an option (or getResources() in Revolution).

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 13416
        • 6 Posts
        I’ve come across wayfinder, but as far as I understood it I can only get one additional field out of it (i.e. I can get id and title or use some other field instead of title).

        Ditto looks more promising, but in Revolution I can’t get the alpha version to work with tags. I’ll see how the stable version works in Evolution.

        I’ll also have a look into getResources, thanks for the tip.
          • 3749
          • 24,544 Posts
          Check out
          $modx->getTree($id, $depth)


          $id is the ID of the resource at the head of the tree you want.
          $depth is the number of levels to include

          It returns an array of IDs (Revolution only)
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 13416
            • 6 Posts
            Thanks, that’s what I was looking for.
            [edit]I would suggest adding an @see getTree to the phpdoc of getChildIds[/edit]
              • 3749
              • 24,544 Posts
              Good idea. Would you please file that as an "improvement" via the Jira link at the top of this page?
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 13416
                • 6 Posts
                  • 3749
                  • 24,544 Posts
                  Thanks. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting