Needs specifics in menu.html.twig—clarify in documentation

Created on 28 January 2023, almost 2 years ago

This module seems to require specific code in menu.html.twig in order for the "active" class to appear in code and to get the expected result of highlighting a menu trail.

Themes stable9 and claro don't seem to have that in their code, the same with /core/modules/system/templates/menu.html.twig.

The theme that seems to have it is Classy, I copied from there into my own theme and I got the trail to work.

It would be great to have that noted on the project page and README.md, either under Requirements or Configuration.

Maybe something like:

Make sure your theme renders the active trail in menu.html.twig. Without that code no class is output that marks the active item and it can seem as if this module isn't working. Some popular themes like stable and claro aren't rendering the active trail in that template so you need to use another theme or apply the necessary changes to your subtheme in order for menu trails to work.

Here is an example of a working code from menu.html.twig:

    {% for item in items %}
      {%
        set classes = [
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      <li{{ item.attributes.addClass(classes) }}>
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      </li>
    {% endfor %}

Change the menu-item--active-trail to the class appropriate to your theme.

📌 Task
Status

Active

Component

Code

Created by

🇭🇷Croatia portulaca

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024