Use an extra to determine what level a resource is in the resource tree

Is there any way to determine the level deep in the resource tree for a resource via an extra. I am developing a template that will use the IF or SWITCH extra to determine what chunk to use.

Depending on the level deep I will use different pdoMenu calls for navigation.

For example in pseudo code:

[[!If?
   &subject=`DEPTH`
   &operator=`EQ`
   &operand=`1`
   &then=`[[$Level-1-chunk]]`
   &else=`[[$Level-2-chunk]].`
]]

The chunks will have pdoMenu calls with different parameters passed to the snippet such as classes and/or tpl chunks.

Thanks

Roy

Hey @rdegler …

Try creating a new snippet called “getLevel” with the following:

<?php
$id = $modx->resource->get('id');
$levelcount = count($modx->getParentIds($id));
$output = $levelcount;
return $output;

Then call the snippet from within a resource to find its level:

[[getLevel]]

in your pseudo code:

[[!If?
   &subject=`[[getLevel]]`
   &operator=`EQ`
   &operand=`1`
   &then=`[[$Level-1-chunk]]`
   &else=`[[$Level-2-chunk]].`
]]

or using output modifiers:

[[getLevel:is=`1`:then=`[[$Level-1-chunk]]`:else=`[[$Level-2-chunk]]`]]

Edit:

If you are using this inside a pdoMenu loop you’ll probably want to pass the menu item resource id to the snippet instead of getting the current resource id - for example:

[[getLevel? &id=`[[+id]]`]]

and remove the first line of the snippet:

<?php
// $id = $modx->resource->get('id');
$levelcount = count($modx->getParentIds($id));
$output = $levelcount;
return $output;
2 Likes

Dejaya,

This is exactly what I was looking for. Thank you so much!!

Roy

1 Like

Glad it’s been a help! :+1:

Wasn’t there a level placeholder?

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.