We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    I slightly changed the internal $documentListing and $aliasListing variables that are generated into and read from the siteCache file, plus added a new one called $documentMap that was helpful in the development of two new API functions...

    $modx->getParentIds($id, $parents= array())
    $modx->getChildIds($id, $parents= array())

    These functions each return an array of all parents or children document ids, having alias (with full alias path in the form topParentAlias/parentAlias/docAlias) as the keys, starting from the document $id you pass to it. No database queries are used to build these, so there is no significant overhead in using these functions.

    Victor used these functions already to build a multilanguage version of the FlexSearchForm snippet that can search through a single branch of the document tree.
      • 1764
      • 680 Posts
      Very cool! So does getChildIds return children from multiple levels (children, grandchildren, etc.) or just the immediate children?

      This is definitely going to be very handy. I’ll overhaul the @INHERIT binding to use getParentIds
        • 25663 MODX Staff
        • 12,272 Posts
        Jason, how does it work?

        In my mind, I see it as an array of all docs in the genealogy for each document ordered in some fashion. If a doc is inserted in at a certain level, will it also update the siblings and parents (and higher) up the genealogy chain?

        Does it support the different ordering methods and menu status used in snippets like DropMenu? (IOW, can we simplify and make DropMenu faster, not that it’s particularly slow... but...)
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 22303 MODX Staff
          • 10,725 Posts
          Right now, these methods are only to return a simple set of all parents or all children from the current node. This is simply a shortcut to getting a collection of all these document id’s, so you can, for instance, build a quick IN() clause for the FlexSearchForm snippet by implode()’ing the results. Also, having the full alias path as the index of the resulting array might come in handy for menu snippets. And between the documentListing, aliasListing, and documentMap metadata now available in the DocumentParser class, we should be able to author whatever additional API methods we need to make menu snippets more efficient. And if you’ve got ideas on how to structure the data better so it can be more effectively utilized, please feel free to share.

          Some future enhancements to the methods, or additional methods might include

          • a limit on the number of levels to get results from
          • optional multi-dimensional array results, representing the tree structure
          • optional sorting parameters to resort the resulting list by whatever criteria you want

          Here is a quick snippet to view this and some of the related data offered up by the current system in a fairly readable format:

          $output= "";
          $output.= "<p>DocumentMap:<br />";
          foreach ($modx->documentMap as $key=>$value) {
          $output.= "$key [ ";
          foreach ($value as $k=>$v) {
          $output.= "$k=>$v";
          }
          $output.= " ]<br />";
          }
          $output.= "</p>";
          $output.= "<p>Parents:<br />";
          foreach ($modx->getParentIds($modx->documentIdentifier) as $docAlias=>$docId) {
          $output.= "  $docAlias=>$docId<br />";
          }
          $output.= "</p>";
          $output.= "<p>Children:<br />";
          foreach ($modx->getChildIds($modx->documentIdentifier) as $docAlias=>$docId) {
          $output.= "  $docAlias=>$docId<br />";
          }
          $output.= "</p>";
          $output.= "<p>DocumentListing:<br />";
          foreach ($modx->documentListing as $docAlias=>$docId) {
          $output.= "  $docAlias=>$docId<br />";
          }
          $output.= "</p>";
          $output.= "<p>AliasListing:<br />";
          foreach ($modx->aliasListing as $docAlias=>$docId) {
          $output.= "$docAlias [ <br />";
          foreach ($docId as $key=>$value) {
          $output.= "  $key=>$value, <br />";
          }
          $output.= " ]<br />";
          }
          $output.= "</p>";
          
          
          return $output;
            • 25663 MODX Staff
            • 12,272 Posts
            I think the future method of tree structure combined with sorting would be what really does the trick. Thanks for the info. smiley
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 22303 MODX Staff
              • 10,725 Posts
              Key point is, all the data we need should be there now and this isn’t supposed to build the menu for you. It’s just an additional data structure and some example methods that make use of it. Your menu snippets can use any of this metadata to do what they need to as far as building the tree, sorting, etc. This type of code really shouldn’t bloat the core.