Say that I have the following navigation:
Home
Internal
Folders
New Folder
Configuration
Categories
New
Tags
New
Options
New
Contact
External
The code I used in my layout.phtml to show this menu:
$internal = $this->navigation('navigation')->findOneByLabel('Internal');
echo $this->navigation('navigation')
->menu()
->setUlClass('nav nav-list')
->setMaxDepth(1)
->renderMenu($internal);
so it shows like this:
Folders
New Folder
Configuration
Categories
Tags
Options
Contact
At the moment I'm getting a decent menu, showing all the parents and the first childs, so the 'New' navigation is never showing.
However, if I'm on the page 'Categories' I want to show their childs too, so the 'New' under the 'Categories' should be showing, like the following:
Folders
New Folder
Configuration
Categories
New
Tags
Options
Contact
I have tried many ways to try this, and have checked all the options (setMinDepth, setMaxDepth, renderSubMenu, setParentMenu) you can give to the menu in the layout.phtml, without succes. It's either the 'New' included, or not included, not something in between.
The documentation about ZF2 is not giving me anything about how to get this sub menu working, apart from making a partial.
So, is making a partial the only option for this?
Thanks in advance!
For a small project a while ago, I have made something as a really quick fix. We've been planning to clean up the code and make it configurable, but we never get to another project where we required the same.
TL;DR: You cannot use the normal zf2 view helper but you have to look up the "top" level page you want to display (Internals in your case) and display the menu below that page.
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class SubNavigation extends AbstractHelper
{
public function __invoke($class = null)
{
$view = $this->getView();
$menu = $view->navigation()->menu();
$container = $view->navigation()->getContainer();
$active = $view->navigation()->setRenderInvisible(true)->findActive($container);
if (!$active) {
return;
}
$container = $active['page'];
$depth = $active['depth'];
while (0 !== $depth) {
$container = $container->getParent();
$depth--;
}
$visible = $container->isVisible();
$html = $menu->setContainer($container->setVisible(true))
->setUlClass('')
->setOnlyActiveBranch(false)
->setMinDepth(null)
->setMaxDepth(null)
->render();
$container->setVisible($visible);
if (strlen($html)) {
return sprintf('<ul %s><li%s><a href="%s">%s</a>%s</li></ul>',
(null !== $class) ? ' class="' . $class . '"' : null,
($container->isActive())? ' class="active"' : null,
$container->getHref(),
$container->getLabel(),
$html);
}
}
}
This view helper renders the "Internal" as a top menu-item in the sprintf()
call, but you can easily alter this behaviour. You can just echo $html
and then it's what you actually need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With