<![CDATA[ getResources: listing resources of a certain level - My Forums]]> https://forums.modx.com/thread/?thread=95675 <![CDATA[getResources: listing resources of a certain level]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517876
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`!].]]>
viener Jan 05, 2015, 05:13 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517876
<![CDATA[Re: getResources: listing resources of a certain level]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517925 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.]]>
viener Jan 06, 2015, 03:45 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517925
<![CDATA[Re: getResources: listing resources of a certain level (Best Answer)]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517888
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.]]>
Bruno17 Jan 05, 2015, 10:03 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517888
<![CDATA[Re: getResources: listing resources of a certain level]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517886
So, announcements chunk with &parents=`[[UltimateParent? &topLevel=`2`]]` called on various level pages gives various relults. And none on the first page (id=1).]]>
viener Jan 05, 2015, 07:46 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517886
<![CDATA[Re: getResources: listing resources of a certain level]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517883
&parents=`[[UltimateParent? &topLevel=`2`]]` //root = level 1

http://modx.com/extras/package/ultimateparent
https://github.com/splittingred/UltimateParent]]>
davidpede Jan 05, 2015, 06:26 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517883
<![CDATA[Re: getResources: listing resources of a certain level]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517882 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.]]>
viener Jan 05, 2015, 06:14 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517882
<![CDATA[Re: getResources: listing resources of a certain level]]> https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517880
&depth=`2`
is the getResource equivalent.]]>
davidpede Jan 05, 2015, 05:40 AM https://forums.modx.com/thread/95675/getresources-listing-resources-of-a-certain-level#dis-post-517880