We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4414
    • 63 Posts
    Hi all,

    I am trying to figure out how to have getResources sort resources according to their appearance in the menu tree. &sortby=`menuitem` did not work for me and produced no output at all.

    Below are my current snippet call and template chunk:
    [[getResources? 
    &tpl=`CHILLY-slide` 
    &parents=`2` 
    &includeTVs=`1`
    &sortby=`menuindex`
    ]]


    Template chunk [[$CHILLY-slide]]
    <div class="slide">
    	<div class="caption">
    		<h2>[[+pagetitle]]</h2>
    		<p>
    		[[+introtext]]
    		</p>
    	</div>
       <div class="sImCon"><img src="[[+tv.image]]" />
       </div>
    </div>


    Cheers,

    Stefan
      • 4513
      • 16 Posts
      Hello,
      maybe try this:
      paste this chunk of code to Ditto’s calling
      &orderBy=`menuindex asc`

      cheers
        • 4414
        • 63 Posts
        Hi callou,

        &sortby=`menuindex` is actually working fine. However, I am looking for a way to sort the resource output according to their appearance in the menu tree. This way I could match the menu tree’s order which in my case is different from the order of the resource IDs.

        Cheers,

        Stefan
          • 22303 MODX Staff
          • 10,725 Posts
          At the current time, you can’t sort the results of getResources in a hierarchical tree like you can with Wayfinder. I am working on adding advanced custom sorting capabilities though for the next release of this snippet, which will allow this.
            • 4414
            • 63 Posts
            That would be great indeed! I really like this snippet.

            Thanks for your efforts!
              • 7557
              • 61 Posts
              Quote from: OpenGeek at Dec 28, 2009, 11:49 AM

              At the current time, you can’t sort the results of getResources in a hierarchical tree like you can with Wayfinder. I am working on adding advanced custom sorting capabilities though for the next release of this snippet, which will allow this.
              Hi!
              Old post I know, but still not posibble order by tree?
              If I missed something? I try with menuitem but that not preserve tree order.
              That’s why I created an custom snippet to be able to sort the resources by tree with getResources (most important snippet for Revo).
              Call it getResourcesTree wink
              <?php
              if (!function_exists('multiarray_keys')) {
                  function multiarray_keys($ar) {
                      foreach ($ar as $k => $v) {
                          $keys[] = $k;
                          if (is_array($ar[$k]))
                              $keys = array_merge($keys, multiarray_keys($ar[$k]));
                      }
                      return $keys;
                  }
              }
              $parents = (!empty($parents) || $parents === '0') ? explode(',', $parents) : array($modx->resource->get('id'));
              $depth = isset($depth) ? (integer) $depth : 10;
              
              $tree = $modx->getTree($parents, $depth);
              $tree = multiarray_keys($tree);
              $tree = implode(',', $tree);
              $tree = 'FIELD(modResource.id, ' . $tree . ')';
              return $tree;
              


              And call getRessources like this:
              [[!getResources? 
              &parents=`19,25` 
              &tpl=`myTpl` 
              &sortdir=`ASC` 
              &limit=`50`   
              &sortby=`[[!getResourcesTree? &parents=`19,25` &depth=`100`]]
              `]]

              I think that depth from getResources, and depth from getResourcesTree($modx->getTree) do not have the same behavior.
                • 26555
                • 38 Posts
                @dj13:
                Nice, just what I was looking for. Works like a charme using MODx Revolution 2.0.8-pl (traditional).
                  • 14162
                  • 67 Posts
                  This is a brilliant snippet and solves my problem (http://modxcms.com/forums/index.php/topic,62275.msg353644.html#msg353644) perfectly. Nice work dj13.
                    • 14125
                    • 62 Posts
                    Fantastic!
                      • 34120
                      • 236 Posts
                      Sorry for the dredge, does anyone know if getResources supports order by tree yet?
                      I'll give dj13's snippet a go otherwise.

                      Cheers