ModX Revolution: sortby folder menuindex then element menuindex

304 Views Asked by At

I am new to ModX. I have resources nested in one another like this:

resource1
  - resource1-child1
  - resource1-child2
resource2
  - resource2-child1
  - resource2-child2

and this code:

[[!getResources? 
&includeContent=`1` 
&parents=`[[pdoField? 
    &id=`[[*id]]` 
    &field=`id` 
    &topLevel=`4`]]` 
&resources=`-[[*id]]` 
&includeTVs=`1` 
&processTVs=`1`
&sortby=`menuindex`
&sortdir=`asc`
&depth=`10` 
&limit=`100` 
&tpl=`allDoctors` 
&where=`{"template:=":104}`]]

But for some reason it does not sort elements by menuindex. It looks like it doesn't sort anything at all unless I extract the children out of parent resources. How can I make it sort everything by folders menu index and then children menuindex? Thanks in advance

1

There are 1 best solutions below

0
On

Found solution myself here by creating a snippet named getResourcesTree

<?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 sorted out like this:

[[!getResources? 
                    &includeContent=`1` 
                    &parents=`[[pdoField? 
                        &id=`[[*id]]` 
                        &field=`id` 
                        &topLevel=`4`]]` 
                    &resources=`-[[*id]]` 
                    &includeTVs=`1` 
                    &processTVs=`1`
                    &sortby=`[[!getResourcesTree? 
                        &parents=`[[pdoField? 
                            &id=`[[*id]]` 
                            &field=`id` 
                            &topLevel=`4`]]` 
                        &depth=`100`]]`
                    &sortdir=`asc`
                    &depth=`10` 
                    &limit=`100` 
                    &tpl=`allDoctors` 
                    &where=`{"template:=":104}`]]