Refactor the macro on menu.html.twig for reusability

Created on 17 May 2015, about 10 years ago
Updated 1 August 2025, 4 days ago

Follow-up to #2443361: Remove theme_book_link, make book tree align with MenuLinkTree build β†’ , where the need for this became obvious.

Problem/Motivation

The template file core/modules/system/templates/menu.html.twig includes this twig macro:

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      <ul{{ attributes.addClass('menu') }}>
    {% else %}
      <ul class="menu">
    {% endif %}
      {% for item in items %}
        <li{{ item.attributes }}>
          {{ link(item.title, item.url) }}
          {% if item.below %}
            {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
          {% endif %}
        </li>
      {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

This is very useful for rendering not only menus, but any tree of links. However, this macro is not currently reusable, as this file is actually a template.
We could all make good use of the code if we tackle this, both within core and contrib.

See documentation on twig macros: http://twig.sensiolabs.org/doc/tags/macro.html

Proposed resolution

Exact solution is up for discussion. But any solution must be re-usable by book-tree.html.twig and menu--toolbar.html.twig at the very least.

Remaining tasks

  • Generalize the macro. Ensure backwards-compatibility with old version.
  • Figure out where else we can use this in core. For now we know:
    • core/modules/book/templates/book-tree.html.twig
    • core/modules/system/templates/menu.html.twig
    • core/modules/toolbar/templates/menu--toolbar.html.twig
    • + core/themes/stable versions of the above
    • core/themes/classy/templates/navigation/book-tree.html.twig
    • core/themes/classy/templates/navigation/menu.html.twig
    • core/themes/classy/templates/navigation/menu--toolbar.html.twig
  • Make the changes on all those places.

User interface changes

None

API changes

None, adding a general core twig macro, which means a new tool for themers to use.

Beta phase evaluation

πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

theme system

Created by

πŸ‡ͺπŸ‡ΈSpain manuel garcia

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024