10

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
    }
}
Sonny
  • 8,204
  • 7
  • 63
  • 134

3 Answers3

3
   $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?

Community
  • 1
  • 1
Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
  • 1
    I don't understand why I can't re-use the `menu()` function and `..._Menu` class name like I do in other situations in the Zend Framework. I have to use a different name. – Sonny Mar 02 '10 at 19:05
  • I'm going to accept your solution as the answer, but I wouldn't mind if you explained why I have to use a different name when extending a view helper. – Sonny Mar 02 '10 at 20:03
  • I'm not sure, but I think this is due Zend_View_Helper_Navigation->__call() / findHelper() method which look for the helper first in the Zend library. So, to reuse the menu method you can call parent::menu(...). – Keyne Viana Mar 02 '10 at 22:08
1

For anyone who might need an answer I found a better way and probably the intended way.

The only thing you have to do is to create your own custom view helper that extends 'Zend_View_Helper_Navigation_HelperAbstract' and set default proxy for navigation view helper to your own.

E.g.

class Admin_View_Helper_NavigationMenu extends
                                     Zend_View_Helper_Navigation_HelperAbstract {

    public function render(\Zend_Navigation_Container $container = null) {
        return "Hello world!!";
    }

}

and

$this->view->navigation()->setDefaultProxy("navigationMenu");

(I am changing default proxy in menu controller action, as it is added to action stack)

Having done that, it will be possible to use this in the view

<?= $this->navigation()->render(); ?>

Note: You still have to rename the view helper class, but that's how view helpers work in Zend (Names are not supposed to collide).

FDIM
  • 1,981
  • 19
  • 21
  • I tried to update to Zend Framework 1.12, and my navigation broke. I haven't had the time to revisit it, but maybe your solution will allow me to fix the issue. Thanks! – Sonny May 23 '13 at 13:48
0

did you edit your post? It seems like my answer is totally irrelevant to your question now?


If you say what you need to change it will be easier. At the moment your question is a little confusing.

I've assumed you want to edit the view AFTER you have already created your navigation. If you are able to do it before you create it, then its even easier. This bit below is a little confusing because you would normally change the options before hand.

// Get the helper navigation
$navigation = $viewRenderer->
                       view->
                       getHelper( 'navigation' )
                      ->menu()
                      ->renderMenu(
                    $YOUR_NAVIGATION_OBJECT,                                
                    array(  'minDepth' => null,
                        'maxDepth' => null,
                        'onlyActiveBranch' => false,
                        'renderParents'    => false,
                        // More options here


                    )                           

);

excuse the indentation, it was really hard to get it lined nearly

Notice that I used $YOUR_NAVIGATION_OBJECT above. You only use that if you use more than one Navigation on your page. Otherwise, you jus use Render() instead of RenderMenu().

Layke
  • 51,422
  • 11
  • 85
  • 111
  • I edited my post to address your concern with my question being confusing. I had already narrowed down what I needed to change. My original question still stands: "How do I extend the Zend Navigation Menu View Helper?" – Sonny Mar 02 '10 at 16:57
  • I see. Okay. I will take a look, but I don't think I know the answer to this one. – Layke Mar 02 '10 at 17:00