KnpMenuBundle icon indicating copy to clipboard operation
KnpMenuBundle copied to clipboard

Current Item not selected

Open 1ukaz opened this issue 13 years ago • 8 comments

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 !!!

1ukaz avatar Mar 21 '13 15:03 1ukaz

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

stof avatar Mar 21 '13 15:03 stof

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 !!!

1ukaz avatar Mar 21 '13 16:03 1ukaz

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.

stof avatar Mar 21 '13 16:03 stof

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 !!

1ukaz avatar Mar 21 '13 17:03 1ukaz

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.

schulluk avatar Apr 22 '13 19:04 schulluk

@lspcity you should, in your menu builder, do something like:

$menu = $factory->createItem('root');
$menu->setCurrentUri($this->container->get('request')->getRequestUri());

Hope it helps

1ukaz avatar Apr 22 '13 20:04 1ukaz

Thank you, this works great. Maybe this would be a good information in the docs...

schulluk avatar Apr 23 '13 11:04 schulluk

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

stof avatar Apr 23 '13 13:04 stof