navigation-menu.html.twig

Same filename in other branches
  1. 11.x core/modules/navigation/templates/navigation-menu.html.twig
{% import _self as menus %}
{% set menu_heading_id = 'menu--' ~ menu_name|clean_unique_id %}
<div class="admin-toolbar__item">
  <h4 id="{{ menu_heading_id }}" class="toolbar-block__title visually-hidden focusable">{{ title }}</h4>
  <ul class="toolbar-block__list" aria-labelledby="{{ menu_heading_id }}">
    {{ menus.menu_items(items, attributes, 0) }}
  </ul>
</div>

{% macro menu_items(items, attributes, menu_level) %}
  {% for item in items %}

    {% set item_link_tag = 'a' %}

    {% if item.url.isRouted %}
      {% if item.url.routeName == '<nolink>' %}
        {% set item_link_tag = constant('\\Drupal\\Core\\GeneratedNoLink::TAG') %}
      {% elseif item.url.routeName == '<button>' %}
        {% set item_link_tag = constant('\\Drupal\\Core\\GeneratedButton::TAG') %}
      {% endif %}
    {% endif %}

    {% if item.url.options.attributes is empty %}
      {% set item_link_attributes = create_attribute() %}
    {% else %}
      {% set item_link_attributes = create_attribute(item.url.options.attributes) %}
    {% endif %}

    {% set item_id = ('navigation-link--' ~ item.original_link.pluginId)|clean_unique_id %}
    {% if menu_level == 0 %}
      {% if item.below is empty %}
        <li id="{{ item_id }}" class="toolbar-block__list-item">
          {% include 'navigation:toolbar-button' with {
            attributes: item_link_attributes.setAttribute('href', item.url|render|default(null)).setAttribute('data-drupal-tooltip', item.title).setAttribute('data-drupal-tooltip-class', 'admin-toolbar__tooltip'),
            icon: item.class|clean_class,
            html_tag: item_link_tag,
            text: item.title,
            modifiers: [
              'collapsible',
              item_link_tag == 'span' ? 'non-interactive' : null
            ]|filter(v => v is not null),
          } only %}
        </li>
      {% else %}
        <li id="{{ item_id }}" class="toolbar-block__list-item toolbar-popover" data-toolbar-popover>
          {% include 'navigation:toolbar-button' with {
            action: 'Extend'|t,
            attributes: create_attribute({
              'aria-expanded': 'false',
              'aria-controls': item_id,
              'data-toolbar-popover-control': true,
              'data-has-safe-triangle': true,
            }),
            icon: item.class|clean_class,
            text: item.title,
            modifiers: [
              'expand--side',
              'collapsible',
            ],
            extra_classes: [
              'toolbar-popover__control',
            ],
          } only %}
          <div class="toolbar-popover__wrapper" data-toolbar-popover-wrapper inert>
            {% if item.url %}
              {% include 'navigation:toolbar-button' with {
                attributes: item_link_attributes.setAttribute('href', item.url|render),
                html_tag: item_link_tag,
                text: item.title,
                modifiers: [
                  'large',
                  'dark',
                ],
                extra_classes: [
                  'toolbar-popover__header',
                ],
              } only %}
            {% else %}
              {% include 'navigation:toolbar-button' with {
                attributes: create_attribute(),
                modifiers: [
                  'large',
                  'dark',
                  'non-interactive',
                ],
                extra_classes: [
                  'toolbar-popover__header',
                ],
                html_tag: 'span',
                text: item.title,
              } only %}
            {% endif %}
            <ul class="toolbar-menu toolbar-popover__menu">
              {{ menus.menu_items(item.below, attributes, 1) }}
            </ul>
          </div>
        </li>
      {% endif %}

    {% elseif menu_level == 1 %}
      <li class="toolbar-menu__item toolbar-menu__item--level-{{ menu_level }}">
        {% if item.below is empty  %}
          {% include 'navigation:toolbar-button' with {
            attributes: item_link_attributes.setAttribute('href', item.url|render|default(null)),
            text: item.title,
            html_tag: item_link_tag,
            extra_classes: [
              item_link_tag == 'span' ? 'toolbar-button--non-interactive'
            ],
          } only %}
        {% else %}
          {% include 'navigation:toolbar-button' with {
            attributes: create_attribute({
              'aria-expanded': 'false',
              'data-toolbar-menu-trigger': menu_level,
            }),
            text: item.title,
            modifiers: ['expand--down'],
          } only %}
          <ul class="toolbar-menu toolbar-menu--level-{{ menu_level + 1 }}" inert>
            {{ menus.menu_items(item.below, attributes, menu_level + 1) }}
          </ul>
        {% endif %}
      </li>
    {% else %}
      <li class="toolbar-menu__item toolbar-menu__item--level-{{ menu_level }}">
        {% if item.below is empty  %}
          {{ link(item.title, item.url, {
            'class': [
              'toolbar-menu__link',
              'toolbar-menu__link--' ~ menu_level,
            ],
            'data-index-text': item.title|first|lower
          }) }}
        {% else %}
          <button
            class="toolbar-menu__link toolbar-menu__link--{{ menu_level }}"
            data-toolbar-menu-trigger="{{ menu_level }}"
            aria-expanded="false"
            data-index-text="{{ item.title|first|lower }}"
          >
            <span data-toolbar-action class="toolbar-menu__link-action visually-hidden">{{ 'Extend'|t }}</span>
            <span class="toolbar-menu__link-title">{{ item.title }}</span>
          </button>
          <ul class="toolbar-menu toolbar-menu--level-{{ menu_level + 1 }}" inert>
            {{ menus.menu_items(item.below, attributes, menu_level + 1) }}
          </ul>
        {% endif %}
      </li>
    {% endif %}
  {% endfor %}
{% endmacro %}
1 theme call to navigation-menu.html.twig
ShortcutLazyBuilder::lazyLinks in core/modules/navigation/src/ShortcutLazyBuilder.php
The #lazy_builder callback; builds shortcut navigation links.

File

core/modules/navigation/templates/navigation-menu.html.twig

View source
  1. {% import _self as menus %}
  2. {% set menu_heading_id = 'menu--' ~ menu_name|clean_unique_id %}
  3. <div class="admin-toolbar__item">
  4. <h4 id="{{ menu_heading_id }}" class="toolbar-block__title visually-hidden focusable">{{ title }}</h4>
  5. <ul class="toolbar-block__list" aria-labelledby="{{ menu_heading_id }}">
  6. {{ menus.menu_items(items, attributes, 0) }}
  7. </ul>
  8. </div>
  9. {% macro menu_items(items, attributes, menu_level) %}
  10. {% for item in items %}
  11. {% set item_link_tag = 'a' %}
  12. {% if item.url.isRouted %}
  13. {% if item.url.routeName == '<nolink>' %}
  14. {% set item_link_tag = constant('\\Drupal\\Core\\GeneratedNoLink::TAG') %}
  15. {% elseif item.url.routeName == '<button>' %}
  16. {% set item_link_tag = constant('\\Drupal\\Core\\GeneratedButton::TAG') %}
  17. {% endif %}
  18. {% endif %}
  19. {% if item.url.options.attributes is empty %}
  20. {% set item_link_attributes = create_attribute() %}
  21. {% else %}
  22. {% set item_link_attributes = create_attribute(item.url.options.attributes) %}
  23. {% endif %}
  24. {% set item_id = ('navigation-link--' ~ item.original_link.pluginId)|clean_unique_id %}
  25. {% if menu_level == 0 %}
  26. {% if item.below is empty %}
  27. <li id="{{ item_id }}" class="toolbar-block__list-item">
  28. {% include 'navigation:toolbar-button' with {
  29. attributes: item_link_attributes.setAttribute('href', item.url|render|default(null)).setAttribute('data-drupal-tooltip', item.title).setAttribute('data-drupal-tooltip-class', 'admin-toolbar__tooltip'),
  30. icon: item.class|clean_class,
  31. html_tag: item_link_tag,
  32. text: item.title,
  33. modifiers: [
  34. 'collapsible',
  35. item_link_tag == 'span' ? 'non-interactive' : null
  36. ]|filter(v => v is not null),
  37. } only %}
  38. </li>
  39. {% else %}
  40. <li id="{{ item_id }}" class="toolbar-block__list-item toolbar-popover" data-toolbar-popover>
  41. {% include 'navigation:toolbar-button' with {
  42. action: 'Extend'|t,
  43. attributes: create_attribute({
  44. 'aria-expanded': 'false',
  45. 'aria-controls': item_id,
  46. 'data-toolbar-popover-control': true,
  47. 'data-has-safe-triangle': true,
  48. }),
  49. icon: item.class|clean_class,
  50. text: item.title,
  51. modifiers: [
  52. 'expand--side',
  53. 'collapsible',
  54. ],
  55. extra_classes: [
  56. 'toolbar-popover__control',
  57. ],
  58. } only %}
  59. <div class="toolbar-popover__wrapper" data-toolbar-popover-wrapper inert>
  60. {% if item.url %}
  61. {% include 'navigation:toolbar-button' with {
  62. attributes: item_link_attributes.setAttribute('href', item.url|render),
  63. html_tag: item_link_tag,
  64. text: item.title,
  65. modifiers: [
  66. 'large',
  67. 'dark',
  68. ],
  69. extra_classes: [
  70. 'toolbar-popover__header',
  71. ],
  72. } only %}
  73. {% else %}
  74. {% include 'navigation:toolbar-button' with {
  75. attributes: create_attribute(),
  76. modifiers: [
  77. 'large',
  78. 'dark',
  79. 'non-interactive',
  80. ],
  81. extra_classes: [
  82. 'toolbar-popover__header',
  83. ],
  84. html_tag: 'span',
  85. text: item.title,
  86. } only %}
  87. {% endif %}
  88. <ul class="toolbar-menu toolbar-popover__menu">
  89. {{ menus.menu_items(item.below, attributes, 1) }}
  90. </ul>
  91. </div>
  92. </li>
  93. {% endif %}
  94. {% elseif menu_level == 1 %}
  95. <li class="toolbar-menu__item toolbar-menu__item--level-{{ menu_level }}">
  96. {% if item.below is empty %}
  97. {% include 'navigation:toolbar-button' with {
  98. attributes: item_link_attributes.setAttribute('href', item.url|render|default(null)),
  99. text: item.title,
  100. html_tag: item_link_tag,
  101. extra_classes: [
  102. item_link_tag == 'span' ? 'toolbar-button--non-interactive'
  103. ],
  104. } only %}
  105. {% else %}
  106. {% include 'navigation:toolbar-button' with {
  107. attributes: create_attribute({
  108. 'aria-expanded': 'false',
  109. 'data-toolbar-menu-trigger': menu_level,
  110. }),
  111. text: item.title,
  112. modifiers: ['expand--down'],
  113. } only %}
  114. <ul class="toolbar-menu toolbar-menu--level-{{ menu_level + 1 }}" inert>
  115. {{ menus.menu_items(item.below, attributes, menu_level + 1) }}
  116. </ul>
  117. {% endif %}
  118. </li>
  119. {% else %}
  120. <li class="toolbar-menu__item toolbar-menu__item--level-{{ menu_level }}">
  121. {% if item.below is empty %}
  122. {{ link(item.title, item.url, {
  123. 'class': [
  124. 'toolbar-menu__link',
  125. 'toolbar-menu__link--' ~ menu_level,
  126. ],
  127. 'data-index-text': item.title|first|lower
  128. }) }}
  129. {% else %}
  130. <button
  131. class="toolbar-menu__link toolbar-menu__link--{{ menu_level }}"
  132. data-toolbar-menu-trigger="{{ menu_level }}"
  133. aria-expanded="false"
  134. data-index-text="{{ item.title|first|lower }}"
  135. >
  136. <span data-toolbar-action class="toolbar-menu__link-action visually-hidden">{{ 'Extend'|t }}</span>
  137. <span class="toolbar-menu__link-title">{{ item.title }}</span>
  138. </button>
  139. <ul class="toolbar-menu toolbar-menu--level-{{ menu_level + 1 }}" inert>
  140. {{ menus.menu_items(item.below, attributes, menu_level + 1) }}
  141. </ul>
  142. {% endif %}
  143. </li>
  144. {% endif %}
  145. {% endfor %}
  146. {% endmacro %}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.