AutoNav template hasChildren()

Permalink 1 user found helpful
Hi there,

I am working with a new autonav template I would like to use in one of my themes.

It actually works ok, but I need it to add a extra class parameter.

Let's say that I have the following code (that is what works now)
<div id="menu">
  <ul class="menu">
    <li class="current"><a href="/"><span>Home</span></a></li>
    <li ><a href="/index.php/about/"><span>About</span></a>
    <div><ul>
      <li ><a href="/index.php/about/press-room/"><span>Press Room</span></a>
      <div><ul>
        <li ><a href="/index.php/about/press-room/launch-our-new-site/"><span>Launch our new site!</span></a>
        </li></ul></div></li>
      <li ><a href="/index.php/about/guestbook/"><span>Guestbook</span></a>
    </li></ul></div></li>
    <li ><a href="/index.php/search/"><span>Search</span></a></li>
  </ul>
</div>


But I would like to end up with a code with an extra class added (class="parent") like this code:

<div id="menu">
  <ul class="menu">
    <li class="current"><a href="/"><span>Home</span></a></li>
    <li ><a href="/index.php/about/" class="parent"><span>About</span></a>
    <div><ul>
      <li ><a href="/index.php/about/press-room/" class="parent"><span>Press Room</span></a>
      <div><ul>
        <li ><a href="/index.php/about/press-room/launch-our-new-site/"><span>Launch our new site!</span></a>
        </li></ul></div></li>
      <li ><a href="/index.php/about/guestbook/"><span>Guestbook</span></a>
    </li></ul></div></li>
    <li ><a href="/index.php/search/"><span>Search</span></a></li>
  </ul>
</div>


As you see I am adding the class="parent" to every <a> tag that has a child.

My autonav template code is like this right now:

<?php 
   defined('C5_EXECUTE') or die(_("Access Denied."));
   $aBlocks = $controller->generateNav();
   global $c;
    $output=array(); 
   $output[]='<div id="menu">';
   $output[]='<ul class="menu">';
   $nh = Loader::helper('navigation');
   $isFirst = true;
    $excludeLevel=1000;
   foreach($aBlocks as $ni) {
      $_c = $ni->getCollectionObject();
      if (!$_c->getCollectionAttributeValue('exclude_nav')) {
          $pageLink = false;
            $pageName = $ni->getName();


Help needed please!

Regards
Michael

maar
 
maar replied on at Permalink Reply
maar
Can this be right....

Isn't there anybody that have a idea how to solve this.

Michael
dstew99 replied on at Permalink Reply
dstew99
Hi.
You have a line of code that adds class to the line item.
} else {                $liClass='class="'.$navSelected.'"';


For testing, I would hard code the parent class there.

} else {                $liClass='class="'.$navSelected.' parent"';


and see how it works.
maar replied on at Permalink Reply
maar
This would not put it in to the <a> tag. And it should only reply to the ones who have a submenu (child).

Your suggestion is adding "parent" to "current" - ending up with class="currentparent".
maar replied on at Permalink Reply
maar
I looked in the controller and found the function "hasChildren()" but I do not understand how to use it.

I would really appreciate a hint in the right direction!

Regards Michael
maar replied on at Permalink Reply
maar
I really would appreciate just a little hint right now!
Could you point me to a description of the AutoNav block and its functions - I can't seem to find any technical description of it.

Please...

Michael
maar replied on at Permalink Reply
maar
I have been playing around with the hasChildren() function.

Code example:
foreach($aBlocks as $ni) {
  $pageName = $ni->getName();
  $myChildCnt = $ni->hasChildren();
  echo "NAME: ".$pageName." CHILDREN: ".$myChildCnt."<br />";
}


I don't know what is wrong, but $ni->hasChildren(); is all ways returning "0". But as I understand it should return the number of children.

I quote from controller.php:
/**
* Returns the number of children below this current nav item
* @return int
*/
function hasChildren() {
  return count($this->subNavigationItems);
}


Am I wrong or is there a error somewhere in the AutoNav block?

Michael
intrax replied on at Permalink Reply
to revive this oold thread...

there is indeed an error the hasChildren method is always returning false ! bad shiiit concrete5
jordanlev replied on at Permalink Reply
jordanlev
I'm not sure whether or not that function is even supposed to work in the first place...

But you may need to call that function on $_c instead of $ni (so $_c->hasChildren() instead of $ni->hasChildren()).

Or if that doesn't work either, you could try calling this instead:
$subPage = $_c->getFirstChild();
$has_children = ($subPage instanceof Page);
strefarytmu replied on at Permalink Reply
Short answer: The hasChildren() feature is non-functional, period.

Long answer: I ran find-in-files on the autonav block code, and AutonavBlockItem::hasChildren() is the only place where AutonavBlockItem::subNavigationItems is ever mentioned. It is neither defined in the class body nor assigned to anywhere in Concrete5 code. It is therefore always null, and so count() on it returns 0.
strefarytmu replied on at Permalink Reply
I managed to solve solve this by adding code to the controller to actually populate the subNavigationItems members in items.

Copy /concrete/blocks/autonav/controller.php to /blocks/autonav/controller.php and add this code in AutonavBlockController::generateNav() just before the return statement:

//initialize the subNavigationItems arrays in items
foreach($this->navArray as $k=>&$nav)
   $nav->subNavigationItems=array();
//index items by collection id
$idIndex=array();
foreach($this->navArray as $k=>&$nav)
   $idIndex[$nav->getCollectionObject()->getCollectionID()] = &$nav;
//add children to parents
foreach($this->navArray as $k=>&$nav)
$idIndex[$nav->getCollectionObject()->getCollectionParentID()]
   ->subNavigationItems[] = &$nav;