Cleanup double <ul> in menu.html.twig

Created on 21 August 2019, about 5 years ago
Updated 30 January 2023, over 1 year ago

The menu.html.twig file contain 2 <ul> tags and therefor the indention of the twig file is a bit uncommon. Also, text editors like PHPStorm have difficulties figuring out what the DOM looks like because they can't find a closing tag for the second <ul>.

This issue can be fixed by moving the if test inside the <ul>.

Before:

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      <ul{{ attributes }}>
    {% else %}
      <ul>
    {% 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 %}

After:

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    <ul{{ menu_level == 0 ? attributes }}>
      {% 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 patch also includes a fix for the stable, classy & unami themes.

Feature request
Status

Needs work

Version

10.1

Component
Menu system 

Last updated about 21 hours ago

Created by

🇧🇪Belgium martijn.cuppens

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

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.

  • 🇮🇪Ireland markconroy

    Hi @manojkumar_97, thanks for your patch for D10. However that patch seems to only target the stable9 theme and not the other menu.html.twig files that are part of core, such as /modules/system/templates/menu.html.twig. you can see in the patch in #9 what other templates we need to target.

    Do you want to re-roll your patch with these other templates included and we can test again then?

  • 🇺🇸United States smustgrave

    Also tagging for a change record as other themes or sites may be base theming off one of the ones we change.

Production build 0.71.5 2024