Screenshot of the configuration form for the Menu Manipulator module.

Patch #75 is available for Drupal 9.1.x and it works! You might want to use skip this module and use the patch instead if you only want to filter menu by language.

This module started as a workaround to this 6+ years old issue in core. It helps site builders to easily filter menu by language with a few clicks and it provides developers with a custom service to manipulate menus (see example in code).

Current available manipulators

  • Add custom icon to menu items
  • Filter menu items by current language
  • ...anything else you'd like this module to do? (open an issue)

How to install

composer require drupal/menu_manipulator
drush en menu_manipulator -y

How to use

Go to Admin > Configuration > User Interface.

Filter by language

Select which menus have to be filtered by language. Menu items will be hidden if not translated into the current language OR if they points to an entity which does not have translation for the current language.

Menu icons

Populate the custom list in settings and select which menus should have icons.

Edit your menu items and select an icon from the dropdown list.

Icon will be available in your Twig template as follow:

    {% for item in items %}
      <li{{ item.attributes }}>
        {# If, for any reason, item.icon is empty, you can retrieve it like this: #}
        {% set icon = item['#icon'] ?: item.original_link.getOptions().icon %} 

        {# Embed icon inside link text #}
        {% set link_text %}
          {% if icon %}
            {# Example of an SVG icon as a Twig template #}
            {% include '@yourtheme/icons/icon.html.twig' with { icon: icon } %}

            {# Example of icon as CSS background image #}
            <span class="icon icon--{{ icon }}"></span>

            {# Hide the text #}
            <span class="visually-hidden">{{ item.title }}</span>
          {% else %}
            {# No icon, display the text only #}
            <span class="menu-item-label">{{ item.title|raw }}</span>
          {% endif %}
        {% endset %}

        {{ link(link_text, item.url) }}

        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}

      </li>
    {% endfor %}

Known issues

  • Apply this patch to fix the compatibility issue with SuperFish module

Recommended/complementary modules

Similar projects


Any questions? Found a bug? Need another menu manipulator?
Please create a new issue here.

Project information

Releases