zend-frameworkzend-navigation

Render children of active branch with Zend Navigation view helper


What I am asking is similar to this issue, which is still unresolved.

I'm trying to create a good submenu solution, but I can't seem to get Zend_View_Helper_Navigation_Menu to work with me.

Here is my XML navigation config:

<configdata>
    <nav>
        <index>
            <label>Index</label>
            <uri>/</uri>
            <pages>

                <home>
                    <label>home</label>
                    <uri>/</uri>
                </home>

                <about>
                    <label>about</label>
                    <uri>/about</uri>
                </about>

                <works>
                    <label>works</label>
                    <uri>/works</uri>
                    <pages>
                        <music>
                            <label>music</label>
                            <uri>/works/music</uri>
                        </music>
                    </pages>
                </works>
                <posts>
                    <label>posts</label>
                    <uri>/posts</uri>
                </posts>                
                <admin>
                    <label>admin</label>
                    <uri>/admin</uri>
                    <pages>
                        <login>
                            <label>log in</label>
                            <uri>/admin/login</uri>
                        </login>
                        <settings>
                            <label>settings</label>
                            <uri>/admin/settings</uri>
                        </settings>
                        <register>
                            <label>register</label>
                            <uri>/admin/register</uri>
                        </register>
                        <logout>
                            <label>log out</label>
                            <uri>/admin/logout</uri>
                        </logout>
                    </pages>
                </admin>

            </pages>
        </index>
    </nav>
</configdata>

And here is the relevant code in my layout:

echo $this->navigation()->menu()->renderMenu(
    null, 
    array(
        'minDepth' => 2, 
        'maxDepth' => 2, 
        'onlyActiveBranch' => true
    )
);

When I navigate to 'admin', I don't see the submenu. If I change the minDepth to 1 I can see that that menu and its children are 'active' in the source. Why are they not being rendered when the minDepth is set to 2?

I did a workaround for this, but I have to copy/paste it into every controller for it to work (because I need the request object in order to do it):

    $pages = $this->view->navigation()->current()->getPages();
    foreach ($pages as $page) {
        $this->_setChildrenInvisible($page);
    }

the _setChildrenInvisible() function:

private function _setChildrenInvisible(Zend_Navigation_Container $container) {
    foreach ($container as $child) {
        if ($child->hasChildren()) {
            $this->_setChildrenInvisible($child);
        }
        $child->setVisible(false);
    }
}

Im just trying to get admin's children to render in the submenu. Does anyone know how to fix this?


Solution

  • In the layout, in the renderMenu method, the renderParents attribute should be set to false

    echo $this->navigation()->menu()->renderMenu(
        null, 
        array(
            'minDepth' => 2, 
            'maxDepth' => 2, 
            'onlyActiveBranch' => true, 
            'renderParents' => false
        )
    );