We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42681
    • 64 Posts
    There are several methods to get the url of the first child of a resource. But which one is the best?

    1. Wayfinder? Probably totally oversized?

    2. GetResources?

    3. GetResourcesFields?

    4. huh
      • 3749
      • 24,544 Posts
      This snippet might do it if you're not too picky about which is the first child (generally, you'll get the first one created):


      $docId = $modx->resource->get('id');
      $children = $modx->getChildIds($docId);
      
      if (! empty($children)) {
         return $modx->makeUrl($children[0], "", "", "full");
      } else {
         return 'No Children';
      }
      


      If you want the first child defined by menuindex, it would be something like this:

      $docId = $modx->resource->get('id');
      $url = 'No Children';
      
      $c = $modx->newQuery('modResource');
      $c->sortby('menuindex', 'ASC');
      $c->select(array('id','parent'));
      $c->limit(1);
      $c->where(array('parent' => $docId));
      
      $childObj = $modx->getCollection('modResource', $c);
      
      if ($childObj) {
          $childId = $childObj->get('id');
          $url = $modx->makeUrl($childId, "", "", "full");
      }
      
      return $url;
      


        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
        • 42681
        • 64 Posts
        Hi Bob,
        thanks a lot!!! But does this mean that there is really no already existing plugin/snippet which returns the url of the first child?

        As we need the first child which is not hidden from the menus can we use for this purpose your second code? Or did I misunderstand your posting?

        And many thanks a lot again!!!
          • 4172
          • 5,888 Posts
          if you prefer to use existing snippets, example with getResources:

          [[getResources? &parents=`XXX` &limit=`1` &sortby=`menuindex` &sortorder=`ASC` &tpl=`resourceUrl`]]


          chunk resourceUrl:

          [[~[[+id]]]]
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
          • You might want to try it with pdoTools and either pdoResources or pdoMenu, as they are more efficient. For example, they don't check permissions unless you specifically tell them to. You can also cut down on the overhead by using an inline tpl, thus no need to load and parse a chunk.
            &tpl=`@CODE [[~[[+id]]]]`
              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
              • 4172
              • 5,888 Posts
              in this case I wouldn't use an inline tpl.

              MODX would try to parse the makeUrl - tag and throw an error, if [[+id]] is empty.

              And [[+id]] will be empty before getResources/pdoResources is running
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 4172
                • 5,888 Posts
                may be, this could work:

                [[[[getResources? &parents=`XXX` &limit=`1` &sortby=`menuindex` &sortorder=`ASC` &tpl=`@INLINE ~[[+id]]`]]]]
                [ed. note: Bruno17 last edited this post 10 years ago.]
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                • Ouch. That is pretty obvious. I think I'd better take a break. I have Cheetos, and this Charlie's Angel's movie...
                    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
                    • 42681
                    • 64 Posts
                    Hi Bruno

                    works perfectly!

                    We only had to add: &sortby=`{"menuindex":"ASC"}`
                    and delete: &sortorder=`ASC`

                    Thanks a lot!!!
                      • 3749
                      • 24,544 Posts
                      Here's an improved version of the snippet above. It assumes that the first child is defined by menuindex and will return the URL of the child with the lowest menuindex. You can also send a &docId property to get the first child of another resource. If you leave that property out, the current resource is assumed. You can also set a &noResult property to set what will be returned if the resource has no children (defaults to an empty string).

                      <?php
                      /* FirstChildUrl Snippet */
                      
                      /* Use current resource if no docId sent */
                      $docId = $modx->getOption('docId', $scriptProperties, $modx->resource->get('id'));
                      
                      /* Set default noResult return */
                      $output = $modx->getOption('noResult', $scriptProperties, '');
                      
                      $c = $modx->newQuery('modResource');
                      $c->sortby('menuindex', 'ASC');
                      $c->select(array('id','parent', 'menuindex'));
                      
                      /* We only want one resource */
                      $c->limit(1);
                      
                      /* Get first child of docId resource */
                      $c->where(array(
                         'parent' => $docId,
                      ));
                      
                      $childObj = $modx->getCollection('modResource', $c);
                      
                      if ($childObj) {
                          $childId = $childObj->get('id');
                          $output = $modx->makeUrl($childId, "", "", "full");
                      }
                      
                      return $url;
                        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