navigation-menu.html.twig

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

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