I met Greg in person a few weeks ago at MODxpo Dallas 2010, and he was gracious enough to offer me a guest post on his blog this week. I’ve been a fan of gregorysmart.com since I began working with MODx about a year and a half ago, not only because it provides valuable information on what I believe is the best-kept secret in content management systems, but also because the information fills an often-overlooked gap: developers who aren’t beginners, but aren’t hardcore programmers, either. He’s inspired me to start my own MODx tips blog/evangelism site, eatsleepmodx.com (watch for more content soon!).
MODx has saved my hide more times than I can remember, mostly because it’s flexible enough to handle whatever obscure problem I throw at it, even though I’m not a PHP ninja. In that spirit, here’s a very basic snippet that I hope someone else will find useful.
What some people don’t realize about the MODx resource tree is that when resources are nested within each other, it’s possible to find the numerical depth of that hierarchy. All you need are two MODx API functions and the PHP count function, like so:
<?php $output = ''; $id = $modx->documentIdentifier; $levelcount = count($modx->getParentIds($id)); $output .= $levelcount; return $output; ?>
If you put this in a snippet and place it on your page or in a template, it will display your current depth, starting at zero.
On its own, this snippet doesn’t do much, but when combined with PHx or incorporated into a wrapper snippet, particularly on a complicated site where using a page’s ID number isn’t always feasible, it can really come in handy. For example, let’s say you have a site where you only want a left-hand menu to appear on pages that are five levels deep in the hierarchy, and you want this menu to show all of the current page’s children (the usefulness of this specific example may be limited, but use your imagination):
<?php
$output = '';
$id = $modx->documentIdentifier;
$levelcount = count($modx->getParentIds($id));
if ($levelcount == 5) {
$wayfinderparams['startId'] = $id;
$output = $modx->runSnippet('Wayfinder', $ wayfinderparams);
return $output;
}
?>
That’s it! It’s simple and may not work for every project, but like most aspects of MODx, it shows that there are always dozens of ways to solve any given problem.
Aaron Ladage
eatsleepmodx.com




Pingback: Snippet to Select Chunks with Wayfinder | Greg Smart