We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    I know this is basic API stuff, but I just can’t seem to figure out how to parse through multiple array levels.

    First, my document structure (simplified) looks like this:

    --Level 1a
    --Level 2a (has children)
    --Level 3a
    --Level 3b
    --Level 3c
    --Level 2b
    --Level 2c
    --Level 1b
    --Level 2a (no children)
    --Level 2b (no children)
    --Level 2c (no children)
    --Level 1c

    Basically, what I’m trying to show is that I have some areas where Level 2 has children, and some where it does not.

    Now, I’m using Ditto to generate the left navigation (long story on why I’m not using Wayfinder, but go with me on this). Within the Ditto &tpl, I’m doing this:

    <li><a href="[[LeftNavLinkGenerator? &id=`[+id+]`]]">[+pagetitle+]</a></li>


    So far, so good. What I want to do now is write the LeftNavLinkGenerator snippet, which will determine if Level 2 has children:

    • If it does, it will output the [+id+] of Level 1 as the link.
    • If it doesn’t, it will output the ID of Level 2 as the link.

    What I’m basically trying to do when there are no children is bypass the Level 1 page. The Level 1 page is an in-between page that shows a list of its own children, so it’s unnecessary if there aren’t any. Here’s a link:

    http://www2.ku.edu/~distinction/cgi-bin/liberal-arts-sciences1

    As you can see, Liberal Arts & Sciences, Applied English, and Pre-Majors are the three that don’t have Level 2 children.

    Can anyone help, please?
      • 813 ☆ A M B ☆
      • 326 Posts
      Never mind -- I figured it out. Well...kinda. It’s a bit more manual than I wanted, but it works.

      First, I created a TV called [*nochildrenredirect*], made it a checkbox, gave it the Input Option Value of "Yes==1", and assigned it to the main page template. In the handful of sections that don’t have children, I checked the checkbox.

      Second, I created this snippet, which gets the TV value of each page and evaluates to see if it’s set as 1. If it is, it does a first-child redirect (borrowed from the FCR snippet -- thanks!). If not, it just returns the ID. Here’s the snippet:

      <?php
      $tvnames = array('nochildrenredirect');
      $tvoutput = $modx->getTemplateVarOutput($tvnames, $id, 1);
      $nochildrenredirect = $tvoutput['nochildrenredirect'];
      
      if ($nochildrenredirect==1) {
          $children= $modx->getActiveChildren($id, 'menuindex', 'ASC');
          $firstChild= $children[0];
          $output= $modx->makeUrl($firstChild['id']);
      }
      else {
          $output= $id;
      }
      return $output;
      ?>


      Not sure if this is useful to anyone else, but I hope it helps!