On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • getResources: is there a way to list resources of a certain level? Like:

    Page1
    |
    SubPage1 - SubPage2 - SubPage3
    |
    SubSubPage1 - SubSubPage2

    - listing the pages of SubSubPage1, SubSubPage2 and all other pages that may happen to be added to SubPage1 or SubPage2 or SubPage3.

    In Evolution there is an extenter, so it is something like [!Ditto? &parents=`1` &extenders=`level` &level=`2` &tpl=`DittoTpl`!].

    This question has been answered by Bruno17. See the first response.

    • I think
      &depth=`2`
      is the getResource equivalent.
        Find me on Twitter, GitHub or Google+
      • Quote from: davidpede at Jan 05, 2015, 11:40 AM
        I think
        &depth=`2`
        is the getResource equivalent.

        - No, depth is a level limit. &depth=`2` means list all resources from level 0, level 1, level 2. But I need to list resources from, for example, level 1 only.
        • Ahh sorry mis-read. In that case you should be able to use 'UltimateParent' to return a ID at the level you want:
          &parents=`[[UltimateParent? &topLevel=`2`]]` //root = level 1

          http://modx.com/extras/package/ultimateparent
          https://github.com/splittingred/UltimateParent
            Find me on Twitter, GitHub or Google+
          • It is not the thing either. My case is like this: I need to show sort of announcements in a sidebar of a web site. These announcements are meant to be excerpts out of pages of a certain level. The sidebar is a chunk called in a template, its content is displayed on every page of any level.

            So, announcements chunk with &parents=`[[UltimateParent? &topLevel=`2`]]` called on various level pages gives various relults. And none on the first page (id=1). [ed. note: viener last edited this post 6 years, 9 months ago.]
            • discuss.answer
              try this:

              snippet 'getParentsAtLevel'

              <?php
              
              //[[getParentsAtLevel? &level=`1` &parents=`0`]]
              $level = $modx->getOption('level', $scriptProperties, 1);
              $parents = explode(',', $modx->getOption('parents', $scriptProperties, $modx->resource->get('id')));
              
              $ids = $parents;
              
              if (!function_exists('getLevelIds')) {
                  function getLevelIds($parents, $level) {
                      global $modx;
                      $ids = array();
                      foreach ($parents as $parent) {
                          $childids = $modx->getChildIds($parent, 1);
                          foreach ($childids as $id) {
                              $ids[] = $id;
                          }
                      }
                      $level = $level - 1;
                      if ($level > 0) {
                          $ids = getLevelIds($ids,$level);
                      }
                      return $ids;
                  }
              }
              
              if ($level > 0) {
                  $ids = getLevelIds($parents,$level);
                  
              }
              $output = implode(',', $ids);
              return $output;
              
              


              call getResources like that:

              &parents=`[[getParentsAtLevel? &level=`1` &parents=`0`]]`


              play a bit with &level to get the correct parents of the wanted level.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
              • Quote from: Bruno17 at Jan 05, 2015, 04:03 PM
                try this:

                snippet 'getParentsAtLevel'

                <!--?php
                
                //[[getParentsAtLevel? &level=`1` &parents=`0`]]
                $level = $modx--->getOption('level', $scriptProperties, 1);
                $parents = explode(',', $modx->getOption('parents', $scriptProperties, $modx->resource->get('id')));
                
                $ids = $parents;
                
                if (!function_exists('getLevelIds')) {
                    function getLevelIds($parents, $level) {
                        global $modx;
                        $ids = array();
                        foreach ($parents as $parent) {
                            $childids = $modx->getChildIds($parent, 1);
                            foreach ($childids as $id) {
                                $ids[] = $id;
                            }
                        }
                        $level = $level - 1;
                        if ($level > 0) {
                            $ids = getLevelIds($ids,$level);
                        }
                        return $ids;
                    }
                }
                
                if ($level > 0) {
                    $ids = getLevelIds($parents,$level);
                    
                }
                $output = implode(',', $ids);
                return $output;
                
                


                call getResources like that:

                &parents=`[[getParentsAtLevel? &level=`1` &parents=`0`]]`


                play a bit with &level to get the correct parents of the wanted level.

                - It works. Thanks a lot indeed!

                I wonder why a feature like this is not an integral part of getResources? Listing pages of a certain level is among the most typical procedures. There is a crutch in Evolution for Ditto - the level extender. Still, why it is not a common part of Ditto, but at least it exists.