Aaron LadageI 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

  • http://twitter.com/netProphET Mike Schell

    Nice solution Aaron, it is a very adaptable idea indeed. Thanks for the new Meccano piece ;-) I look forward to reading your blog.

  • buddyrigotti

    Hi Aaron,
    Interesting post. I'm not sure I understand the application though. Do you have a demo somewhere?

  • http://twitter.com/leeboone Lee Boone

    sweet!

  • http://twitter.com/aladage Aaron Ladage

    A valid question :-) I was wondering when I wrote it if the example would explain it well enough.

    It's useful on very large sites where an entire section of the tree's hierarchy needs to be duplicated, but the old content needs to remain intact (same links, same Wayfinder/Ditto calls, etc.), such as when you're creating content that will eventually become an archive. I can't show you the back-end, but I work for a university and am using it to build our new course catalog site, http://distinction.ku.edu/catalog, which launches this week.

    What you're seeing right now is the 2010-11 catalog (imagine it has an ID# of 25 for example's sake, and all of the pages associated with it are children of it). When it's time to create the 2011-2012 catalog, it's important that everything within 2010-11 stays the same to serve as an archive. By making heavy use of this snippet (along with several other work-arounds that have pretty much driven me crazy for the last four months), all I have to do is choose “Duplicate Resource” on resource ID# 25. The archive will be intact, and we can edit the new (duplicated) catalog without fear of disturbing it.

    Make sense?

  • Mitch

    Hmm, yet, it's impossible to find programmers who can do the job. Either skyrocket pricing or a little less and never do anything anyway …

    With billions of blogs and forums and other sites out there all promoting the professionalism in programming, I really start wondering, what about if I ever meet a pro?

  • James

    I think this is a great idea and can think of tonnes of times this would be useful – first that comes to mind is if you're reusing the same template up and down the site but need a couple of behaviors changed depending on the depth (sidebars).

  • http://www.cheap-nikeshox.com/nike-shox-R5-women.html shox r5 women

    It looks good,I have learn a recruit!Recently,I found an excellent online store, thenrtrsmitters.com XX are completely various, good quality and cheap price,it’s worth buying!

  • 111

    11111111

  • http://www.nikemaxsale.com nike air max

    “your details is really reasonable and unique I very concerned about sports and I believe only sports nike air max can make you energetic. I want to buy some sports thing ,will you into321 give some suggestions for me from below websites ?

  • http://www.nikemaxsale.com nike air max

    “your details is really reasonable and unique I very concerned about sports and I imogen-heap believe only sports can make you energetic. I want to buy some sports thing ,will you give some suggestions nike air max for me from below websites ?

  • Des

    Awesome post. I used the same idea here (http://www.salvationarmycarolinas.org/commands) – where anything a level deeper than “commands” is considered a local office “home page” and I have some snippets in the back doing some magic to make it all work. It's a great idea.

    Looking forward to reading the new blog too and keeping up with your modx-ploits.

  • http://www.air-jordan-21.net air jordan 21

    Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much air jordan 19 good information we can learn in this forum!

  • http://www.discount-coach-outlet.com coach outlet

    “Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum coach outlet when I m free and I find there are so coach handbags much good information we can learn in this forum! ugg boots

  • Pingback: Snippet to Select Chunks with Wayfinder | Greg Smart

  • Anonymous

    Works, thanks!

  • http://www.facebook.com/people/Innokentiy-Sokolov/100001475690330 Innokentiy Sokolov

    Awesome solution! Thank you!

  • http://www.knowarecms.com Erik Domingo

    Thanks Aaron – I have a complex site where the navigation display is dependent on the depth of the current document in the hierarchy. Your code is EXACTLY what I’ve been searching for!