KnpMenuBundle icon indicating copy to clipboard operation
KnpMenuBundle copied to clipboard

Passing arguments to knp_menu_render

Open pulse00 opened this issue 14 years ago • 21 comments

Looking at https://github.com/KnpLabs/KnpMenuBundle/blob/master/Provider/BuilderAliasProvider.php#L58, i guess this is not possible.

Do i need to create a custom renderer to be able to pass an argument to the builder method?

pulse00 avatar Dec 13 '11 15:12 pulse00

the renderer is not responsible to call the builder at all. It receives the menu (optionally from a menu provider) and renders it.

Which sort of arguments do you want to pass exactly ?

stof avatar Dec 13 '11 16:12 stof

i'd like to pass an entity to the builder as the second argument (the first one is the FactoryInterface).

Something like {{ knp_menu_render('MyBuilder:myMethod', entity }} which would correspond to


<?php

 ...

public function myMethod(FactoryInterface $factory, SomeClass $entity)
{
   ...
}

pulse00 avatar Dec 13 '11 17:12 pulse00

This is exactly what I would need. Did you guys figure that out ?

bartlomiej84 avatar Jan 29 '12 12:01 bartlomiej84

same problem, I need to pass a parameter to generate a route.

ejosvp avatar Feb 03 '12 14:02 ejosvp

same problem... will be great if it's possible

ghost avatar Feb 07 '12 23:02 ghost

For my needs, I configured my menu as a service as it is described in docs and inject there my object.

I needed to pass user, so i was quite easy.

bartlomiej84 avatar Feb 07 '12 23:02 bartlomiej84

@pulse00 @bnlab @ejosvp @jorji Please take a look at my implementation (you will need the changes done in the pull requests for both the bundle and KnpMenu). What you asked is possible when getting the menu with knp_menu_get (which can then be passed to knp_menu_render). I chose not to add it in knp_menu_render as well as it would make its signature really complex (and it would have been as fourth argument to maintain BC which may also have been painful)

Please provide feedback for this.

stof avatar Feb 12 '12 16:02 stof

About knp_menu_render. I move some options to menu item and loads from config: https://github.com/zerkalica/MillwrightMenuBundle/blob/master/Twig/Helper.php#L102

Setup it in menu builder:

$menuItem->setRenderOptions(array(
    'template' => 'MillwrightMenuBundle:Default:knp_menu.html.twig',
));

zerkalica avatar Feb 12 '12 16:02 zerkalica

@zerkalica the MenuItem is not responsible for the rendering. You are mixing responsibilities here

stof avatar Feb 12 '12 16:02 stof

Ok, make service with rendering options and use it in helper:

$options += $this->viewOptions->get($menuItem->getName());

zerkalica avatar Feb 12 '12 17:02 zerkalica

How to pass context options to ContainerAwareProvider without accessing the menu builder ?

https://github.com/KnpLabs/KnpMenuBundle/blob/provider_options/Provider/ContainerAwareProvider.php#L25

zerkalica avatar Feb 12 '12 17:02 zerkalica

The ContainerAwareProvider does not use the options. It retrieves the menu from the container and cannot change the service definition at runtime.

stof avatar Feb 12 '12 17:02 stof

Don't use my modifications! Read the two comments after this!


I solved the problem by modifiing line 89 in Knp\Menu\Twig\Helper.php: $menu = $this->get($menu, $path, $options);

When I now call knp_menu_render('AcmeCoreBundle:MenuBuilder:main', {'currentAsLink': true, 'currentClass': 'active', 'compressed': true, 'usergroups': usergroups}) I get also the options with currentAsLink, currentClass, compressed and my usergroups in parameter $options: public function main(FactoryInterface $factory, array $options) { print_r($options["usergroups"]); }

schulluk avatar Apr 27 '13 10:04 schulluk

@lspcity This is wrong. KnpMenu has 2 different sets of options: menu builder options and rendering options. knp_menu_render is about rendering. If you need builder options, you need to use knp_menu_get (which is documented)

stof avatar Apr 27 '13 11:04 stof

Yes, you are right! knp_menu_get was what I need. I reverted my changes! Thank you.

{% set menu = knp_menu_get('AcmeCoreBundle:MenuBuilder:main', [], {'usergroups': usergroups} ) %}
{{ knp_menu_render(menu, {'currentAsLink': true, 'currentClass': 'active', 'compressed': true}) }}

schulluk avatar Apr 27 '13 11:04 schulluk

Thank you, it's a great update, but I don't understand, how can I use these options from Builder?

I've passed options in the Twig template like this: {% set menuBreadCrumbs=knp_menu_get("breadcrumbs",[],{"entity":entity}) %} {{ knp_menu_render(menuBreadCrumbs, {"currentAsLink":false,"template":"RealtimeinHomeBundle:Menu:breadcrumbs.html.twig"}) }}

so in my Builder's method, how can I get and use passed options?

My KnpMenu launched as a service and has only $factory parameter in its constructor....

I'm hardly new to the Symfony and KnpMenu and do not understand really obvious things.. Sorry :(

Maybe I need to extend some class or implement some interface? Which one?

velikanov avatar Jul 16 '13 11:07 velikanov

@velikanov as documented, builder options cannot be used when defining the Menu itself as a service as the menu provider is not responsible to build the menu in this case, the container is. And there is no way to alter the instantiation of a service when retrieving it (which is logical). I have another provider implementation in mind based on the controller, which would get the builder from the container instead of getting the menu itself. It will make more sense too as the menu itself is not a good candidate for being a service and using the container as a factory is abusing it.

If you need options currently, the solution is to use the bundle alias provider instead of the service provider: https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/index.md#method-a-the-easy-way-yay

stof avatar Jul 16 '13 11:07 stof

@stof Thank you for the rapid answer! now I understand why "Method b) A menu as a service" is not listed in "The Easy Way" :))

velikanov avatar Jul 16 '13 11:07 velikanov

With some time spent (without the service killing) I've figured this solution:

WARNING: this solution is hardly rough and can not be used as default TIP: you doesn't need to use findByName - I use it because I'm fetching data from DB by name, not by ID

Anyway, first, I'm passing "@doctrine.orm.entity_manager" as parameter in Builder's method then in the Builder's method I instantiate custom entity repository and current route parameters like this:

$currentRouteParameters=$request->attributes->all()["_route_params"]; $repo=$em->getRepository('AcmeDemoBundle:Demo'); $entity=$repo->findByName($currentRouteParameters["name"]);

And in custom Repository I've created an array that stores already fetched Entity row like this (this is done for 1 single query usage):

class DemoRepository extends EntityRepository { private $foundByName=array(); public function findByName($name) { if(!isset($this->foundByName[$name])) { $this->foundByName[$name]=$this->findOneBy(array ( "name"=>$name )); } return$this->foundByName[$name]; } }

velikanov avatar Jul 16 '13 13:07 velikanov

@stof, can you recommend me a way to achieve this goal in more effective way?

I have a menu tree and Im displaying 3 navigation elements out of it using knp_menu_get and knp_menu_render. When I want to pass to renderer ID of parent element in twig (just for correct schema org markup), I need to use options argument of knp_menu_get. It is all working fine... It's just not efficient - I have to build whole menu tree 3x times to get it done. Options for renderer would be really appreciated, because what I need is only to render the tree branches differently, not rebuild the whole thing all the time.

jkabat avatar Jul 29 '14 12:07 jkabat