0

Trying to theme a menu in Drupal 8, running into a case where we need to add classes on certain links.

In the case of a menu link that is route:<nolink> we need to not add any class at all.

Example:

{% if item.below and item.url %}
 {{ link(item.title, item.url, { 'class':['link', 'link--arrow']} ) }}
{% else %}
 // not a link
{% endif %}

How can we tell at this level that item.url is route:<nolink>?

Update:

Current solution so far, is there a better way?

{% if not item.url.isExternal() and item.url.getRouteName() is same as('<nolink>') %}
  {{ link(item.title, item.url) }}
{% else %}
  {{ link(item.title, item.url, { 'class':['link', 'link--arrow']} ) }}
{% endif %}
0

Might be able to do a toString and if it's not actually a link that will come back empty

{% if item.below and item.url and item.url.toString|length > 0 %}
  • We tried length, but without the toString part. – Kevin Apr 17 at 15:24
  • I think item.url|length would always be greater than 0 even for nolinks as its an array of renderable stuff I think. I've just tried the toString idea and seems to work – Leigh Apr 17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.