Auto Nav dynamic output based on level

Permalink
Here is what my client is wanting to do:
Sub Navigation always shows the "below" level with its immediate children, unless there are no children. If there are no children, then show current level.

here is an example:http://www.pcbc.org/conc/index.php/life-stages/students...

Working link: "Thee Camp 2012" (has no children) works as expected. It shows the parent level.
Working link: "Upcoming Events" (has visible children) works as expected. It shows the below level.
Not Working: "Weekly Activities" (has excluded from nav children) does not show desired results because the page has children and those children are excluded from the nav.

The following code almost works. It breaks down when a page "Weekly Activities" has children that are excluded from the nav.

<?php if ($c->getNumChildren() > 0) {
                   //we have children
                   echo '<h2 style="color:#4E75AC;">'.$c->getCollectionName().'</h2></div><div class="pad">';
                   echo 'we have kids '.$c->getNumChildren();
                    $nav = BlockType::getByHandle('autonav');
                    $nav->controller->orderBy = 'display_asc';
                    $nav->controller->displayUnavailablePages = '0';
                    $nav->controller->displayPages = 'below';
                    $nav->controller->displaySubPages = 'relevant';
                    $nav->controller->displaySubPageLevels = 'custom';
                    $nav->controller->displaySubPageLevelsNum = 2;
                    $nav->render('templates/sub_menu');    
                } else {
                    //no children
                    echo '<h2><a style="color:#4E75AC;text-decoration:none;" href="'.$parent_url.'">'.$parent->getCollectionName().'</a></h2></div><div class="pad">';


Any clues would be very much appreciated!

ministrycrm
 
jero replied on at Permalink Reply
jero
You might take a look athttp://c5blog.jordanlev.com/blog/2011/12/customizing-the-autonav-te... - might be some useful information in there.
ministrycrm replied on at Permalink Reply
ministrycrm
Thank you! Actually, digging around the API I found a key ingredient: getCollectionChildrenArray()

So this is what I came up with.

<?php
//set the menu to current if it has no visible sub pages
  $validchild = 'current';
  $children = $c->getCollectionChildrenArray();
  foreach($children as $value){
    $checknav = Page::getByID($value);
    if(!$checknav->getCollectionAttributeValue('exclude_nav')==1){
//We found a visible subpage, change level
      $validchild = 'below';
      break;
    }
  }  
//Lets print out the correct menu title, if no children print parent page title                              
  print ($validchild == 'current' ? "<h2>".$parent->getCollectionName()."</h2>" : "<h2>".$c->getCollectionName()."</h2>");
  $nav = BlockType::getByHandle('autonav');