Current Item not selected
Hi people, great job !!
I was wondering why {%- if item.current %} is not working on my extension of Knp/Menu/Resources/views/knp_menu.html.twig ??
I know is not working because I put a text inside that condition and it never gets displayed unless i set the item as current from within PHP, at the menu builder ...
I am overriding the whole template, and setting it up as the default one, from the config.yml ...
Any help will be much appreciated !!!
item.current is getting the current flag set on the item to force its current state. It is not getting the final value determined by the matcher (the item does not know about the matcher).
You need to use the matcher to determine if an item is current: https://github.com/KnpLabs/KnpMenu/blob/master/src/Knp/Menu/Resources/views/knp_menu.html.twig#L49
The thing about the matchers is that they are present in the last dev-master version and I am using the LAST STABLE VERSION of the bundle which is the 1.1.* Any other suggestions ?? Thanks a lot !!!
Well, the matching of the current item in 1.x was really bad. It was necessary to take care of it when building the menu, by setting the items as current explicitly or by setting the current request uri in the root menu item.
Yup, that's what I ended up doing ...
setting the current request uri in the root menu item
And it seems to be working, so for me and for now is OK xDD Any ideas when the new version with matchers is going to be released as stable one? Since is required by the SonataAdminBundle; I can't install other version than the stable one xDD Thanks for the help !!
How can I set the URI for the root element?
I tried $menu->setChildrenAttribute("uri", $_SERVER["REQUEST_URI"]); and $menu->setAttribute("uri", $_SERVER["REQUEST_URI"]); but v1.1.* does not set the current class automatically.
@lspcity you should, in your menu builder, do something like:
$menu = $factory->createItem('root');
$menu->setCurrentUri($this->container->get('request')->getRequestUri());
Hope it helps
Thank you, this works great. Maybe this would be a good information in the docs...
The doc for the version 1.1 shows it: https://github.com/KnpLabs/KnpMenu/blob/1.1.x/doc/01-Basic-Menus.markdown#the-current-menu-item