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

Kyna’s picture

Issue summary: View changes
Kyna’s picture

Issue summary: View changes
Kyna’s picture

Issue summary: View changes
star-szr’s picture

Status: Active » Postponed (maintainer needs more info)

So 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-operators

Disclaimer: 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:

<a href="{{ path(item.url.routeName, item.url.routeParameters, item.url.options) }}">{{ item.title }}</a>

Possible solution #2

Potentially you could call methods on the Url object to manipulate the parameters, then just use the regular link() function:

{% do item.url.setRouteParameters({'param1': 'value', 'param2': 'value'}) %}
{{ link(item.title, item.url) }}

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):

<a href="{{ path(item.url.routeName, item.url.routeParameters|merge({'param1': 'value', 'param2': 'value'}), item.url.options) }}">{{ item.title }}</a>

…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#variables

If you absolutely want to do something like this in your template I would probably lean towards #2 if possible.

dawehner’s picture

Just in general I think we could also some up with something like:

url_from_route() similar to Url::fromRoute()

Kyna’s picture

Hello, I try it as soon as possible, thank you.

joelpittet’s picture

Status: Postponed (maintainer needs more info) » Fixed

Question answered, this is old. Triaging so closing.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.