0

I am trying to write a custom twig for menu--main.html.twig under Drupal 8.3. When in the menu_links macro while on the home page, the item.in_active_trail is not TRUE. I tried to compare the path('') to item.url but it never returns true. I can print out both path('') and item.url and see that they output the same exact text (character for character). Is there a special way to determine if the item.url is the front page or to string compare URL strings?

{{ path('<front>') == item.url ? 'true' : 'false' }} ==> 'false'
{{ path('<front>') }} ==> '/drupal8_test/'
{{ item.url }} ==> '/drupal8_test/'
2

To get the path from item.url you would have to use the method toString():

  {{ item.url.tostring }}

Instead of comparing strings a better approach might be to check for the route name of the frontpage:

  {% if item.url.routed and item.url.routename == '<front>' %}
    <em>Front Page</em>
  {% endif %}
  • Thank you very much! The alternative is also very clear and concise! – George Houpis Apr 3 '17 at 12:08

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.