Hello, I would like know how to pass parameters to path and url twig function please, it's for custom a menu inside a personal menu--secondary-.html.twig.
My code :
{% for item in items %}
<li>
<a href="{{ path(item.url.routeName) }}">{{ item.title }}</a>
</li>
{% endfor %}
Drupal 8 doc for menu view :
menu_name: The machine name of the menu.
items: A nested list of menu items. Each menu item contains:
title: The menu link title.
url: The menu link url, instance of \Drupal\Core\Url
And the error :
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("node") to generate a URL for route "/node/{node}".") in "themes/bootstrap/templates/menu--secondary-.html.twig" at line 47.
I understand the error, but don't know how pass the parameters, I have try this code :
{% if item.url.routeParameters %}
{% for key, parameter in item.url.routeParameters %}
{% if loop.first %}
{% set parameters = '"' ~ key ~ '": ' ~ parameter %}
{% else %}
{% set parameters = parameters ~ ', "' ~ key ~ '": ' ~ parameter %}
{% endif %}
{% endfor %}
{% else %}
{% endif %}
{{ path(item.url.routeName, { parameters }) }}
But the function don't accept an array of parameters.
Twig_Error_Syntax: A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "}"
Have you a solution please ? Thanks.
Comments
Comment #1
Kyna CreditAttribution: Kyna commentedComment #2
Kyna CreditAttribution: Kyna commentedComment #3
Kyna CreditAttribution: Kyna commentedComment #4
star-szrSo to start with, I'm not clear on the use case here, expanding on that will help us to understand and give you perhaps a better solution.
In Twig,
~
is only for string concatenation. http://twig.sensiolabs.org/doc/templates.html#other-operatorsDisclaimer: The code below may or may not break your site :) this is the result of spending a few minutes playing around in menu.html.twig.
Possible solution #1
Something like this may work if you want to pull out the guts of the route and manipulate them in your template:
Possible solution #2
Potentially you could call methods on the Url object to manipulate the parameters, then just use the regular link() function:
Possible solution #3
You could also maybe use the merge filter to add your own parameters to the array (haven't tested this one and this is definitely the ugliest solution):
…but I think you should also consider whether or not the theme layer is the right place for this logic (just because you can doesn't mean you should) :) Again, I don't know enough about your end goal here.
A general tip I can give would be to install the Kint module from Devel, then you can see the available methods on an object by putting for example
{{ kint(item.url) }}
in your template. That's how I figured out what to use with Twig's dot notation to call getRouteParameters and getOptions on the Drupal\Core\Url object. More details on how the dot notation works: http://twig.sensiolabs.org/doc/templates.html#variablesIf you absolutely want to do something like this in your template I would probably lean towards #2 if possible.
Comment #5
dawehnerJust in general I think we could also some up with something like:
url_from_route()
similar to Url::fromRoute()Comment #6
Kyna CreditAttribution: Kyna commentedHello, I try it as soon as possible, thank you.
Comment #7
joelpittetQuestion answered, this is old. Triaging so closing.