Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Theming sub-menus in Drupal 8: is_expanded in Twig

Sub-menus can be a challenge to theme and integrate into a Drupal 8 theme.

By default, Drupal may not provide the required CSS classes or other HTML attributes you require for a menu item with children.

Before you start, ensure that the menu block is configured to display sufficient levels of sub-menu items (you can just set this to Unlimited for ease).

In your template file, you can use the following syntax to apply your desired classes or other attributes to an expanded menu item:

{% for item in items %}
  {% if item.is_expanded %}
    <li{{ item.attributes.addClass('your-dropdown-class') }}>
  {% else %}
  <li{{ item.attributes.addClass('your-normal-class') }}>
{% endif %}