cakan
cakan

Reputation: 2117

ZF2 - overriding navigation menu view helper function

I'm trying to extend the Zend\View\Helper\Navigation\Menu helper class with my own and override accept function. Based on ZF2 register custom helper for navigation question, my Menu helper class looks like this:

<?php
namespace Application\View\Helper\Navigation;

use Zend\View\Helper\Navigation\Menu as ZendMenu;
use Zend\Navigation\Page\AbstractPage;

class Menu extends ZendMenu
{
    public function accept(AbstractPage $page, $recursive = true)
    {
        $accept = true;

        return $accept;
    }
}

In module.config.php I have added this:

'navigation_helpers' => array (
    'invokables' => array(
        'menu' => 'Application\View\Helper\Navigation\Menu',
    ),
),

This code works fine in general case, but accept function in my class is not called when a partial layout is used to render menu. The problem might be with calling renderPartial() vs renderMenu() in the background, but I'm not sure how to solve it.

Any help is appreciated.

Upvotes: 1

Views: 482

Answers (1)

jcropp
jcropp

Reputation: 1246

Check out this tutorial by Samsonasik. I use a hack of his example to develop two-dimensional menus. While the meat of the tutorial is about dynamic menus, how he renders the menu may be of help.

Samsonasik calls the menu like this:

<div class="nav-collapse">
<?php echo $this->navigation('Navigation')->menu()->setUlClass('nav'); ?>
</div>

In my hack, I call my menu like this:

<?php

// ...

echo "<div class='collapse navbar-collapse'>";
    echo "<ul class='nav navbar-nav'>";
        echo $this->navigation('mainNavigation')
                  ->menu()
                  ->setPartial('partial/menu')
                  ->render();
    echo "</ul>";
echo "</div>";

Upvotes: 2

Related Questions