Skip to content

Compatibility: menu_link_attributes link classes not rendered in menu--main.html.twig #820

Description

@jjroelofs

Summary

The menu--main.html.twig template hardcodes class="nav-link" (and class="dropdown-item" for submenus) on menu link <a> tags, ignoring any custom link classes set via the menu_link_attributes module.

Steps to reproduce

  1. Enable the menu_link_attributes module (ships with DXPR CMS)
  2. Edit a main menu link and add a Link class such as btn btn-primary
  3. The class is stored correctly in the link options: {"attributes":{"class":["btn","btn-primary"]}}
  4. However, the rendered HTML outputs only class="nav-link" — the custom classes are lost

Root cause

In menu--main.html.twig, the link element is built with a literal class attribute:

{# Line ~109: Simple top-level item #}
<{{ item_link_tag }}
  class="nav-link"
  ...
  role="menuitem">{{ item.title }}</{{ item_link_tag }}>

The URL's options/attributes (which carry the menu_link_attributes classes) are never merged into the <a> tag. This pattern repeats for all link types in the template (split dropdown, full toggle, submenu items).

Expected behavior

Custom link classes from menu_link_attributes should be merged with the base classes. For example, a CTA button in the nav should render as:

<a class="nav-link btn btn-primary" href="/shop" role="menuitem">Pre-order</a>

Suggested fix

Use item.url.getOption('attributes') in the template to merge custom attributes, similar to how item.attributes is used on the <li> element for container_class.

Environment

  • DXPR Theme: latest (1.x branch)
  • Drupal: 11.3.5
  • menu_link_attributes: enabled

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions