How do I extend the Zend Navigation Menu View Helper?

Sonny picture Sonny · Mar 2, 2010 · Viewed 8.6k times · Source

I need to change the output of Zend_View_Helper_Navigation_Menu. I've found the two functions that I'll need to modify, and I know how to make the changes I need. What I don't know is how to make the Navigation object use my view helper instead of the Zend one.

Code snippet representing my class extension:

// file /library/My/View/Helper/Navigation/Menu.php
class My_View_Helper_Navigation_Menu extends Zend_View_Helper_Navigation_Menu
{
    protected function _renderDeepestMenu(Zend_Navigation_Container $container,
                                          $ulClass,
                                          $indent,
                                          $minDepth,
                                          $maxDepth)
    {
        // modified code here
    }

    protected function _renderMenu(Zend_Navigation_Container $container,
                                   $ulClass,
                                   $indent,
                                   $minDepth,
                                   $maxDepth,
                                   $onlyActive)
    {
        // modified code here
    }
}

Edits to Clarify

I want to change the class of the <li> elements and remove the EOL and indentation. There are no options to do that with the menu view script, which is why I'll have to extend it.

Initializing the navigation object in my Bootstrap:

$navTable = new Default_Model_Site_DbTable_Navigation();
$view = $this->getResource('view');
$view->navigation(new Zend_Navigation($navTable->getNavigation()));

Rendering the menu in my layout:

echo $this->navigation()->menu();

Solution

I got it working by renaming things as follows, but I am not clear on why I can't overload/overwrite the _Menu class and menu() function.

  1. Change the class name to My_View_Helper_Navigation_MyMenu
  2. Add the myMenu function to the the class (return parent::menu($container);)
  3. Call echo $this->navigation()->myMenu(); in the layout

Class wireframe:

// file /library/My/View/Helper/Navigation/MyMenu.php
class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
    public function myMenu(Zend_Navigation_Container $container = null)
    {
        return parent::menu($container);
    }

    protected function _renderDeepestMenu(Zend_Navigation_Container $container,
                                          $ulClass,
                                          $indent,
                                          $minDepth,
                                          $maxDepth)
    {
        // modified code here
    }

    protected function _renderMenu(Zend_Navigation_Container $container,
                                   $ulClass,
                                   $indent,
                                   $minDepth,
                                   $maxDepth,
                                   $onlyActive)
    {
        // modified code here
    }
}

Answer

Keyne Viana picture Keyne Viana · Mar 2, 2010
   $view->addHelperPath(
      APPLICATION_ROOT . '/library/MyApp/View/Helper/Navigation',
      'MyApp_View_Helper_'
      );


echo $this->navigation()->myMenu(); // name of your class

From: Getting Zend_Navigation menu to work with jQuery's Fisheye

EDIT

Sorry I've not seen your solution, it is exactly what I've posted.

But why this is not a trully extension of menu class?