ZF-8951: Zend_Navigation View Helper Menu - allow different active classes for active parents


Ideally my output navigation would look something like this:

First Level Second Level

I would like to display active parents differently from the active leaf nodes. Since I cannot use CSS markup for I need a different class for the parent nodes. This is a common use-case for me.

I suggest an amend to the Zend/View/Helper/Navigation/Menu.php

258             // create li element for page
259             $liCss = $page->isActive($recursive)
260                    ? ' class="active"'
261                    : '';


258             // create li element for page
259             $liCss = $page->isActive($recursive)
260                    ? ' class="active"'
261                    : '';
new             // show active parent
new             if (!$recursive && !$page->isActive(false) && $page->isActive(true)) $liCss = ' class="active-parent"';

This will not change the current behaviour for users using setParentActive. Those not using it will get extra classes which will not affect display (unless they happen to be using that class name).

I think this is a very common use-case, so warrants inclusion in the library, rather than simply using a partial menu template.

I suspect my code could be expressed more elegantly. The option could be added as a new configuration parameter.


??I need a different class for the parent nodes.?? I think that would be a BC break in HTML and CSS.

My suggestion:

class="active parent"

In CSS you can write:

I have found a simple solution. I will write some unit tests and the implementation which includes:

  • Two new properties {{$_parentClass}} and {{$_renderParentClass}}
  • Getter and setter for these properties ({{setParentClass()}}, {{getParentClass()}}, {{setRenderParentClass()}}, {{getRenderParentClass()}})
  • Update the methods {{renderMenu()}}, {{_renderMenu()}} and {{_normalizeOptions}}

Patch and unit tests added.

Patch fails to apply

I'm also a little concerned that adding a class will result in unexpected styling. Maybe name the new class something like zf-nav-parent ?

Or "menu-parent"?

Fixed on trunk (25214) and release-1.12 (25215)

Standard parent class is "menu-parent".