You are here

menu--primary-menu.html.twig in Drupal 9

Olivero's theme implementation for the menus in the primary_menu region.

Available variables:

  • menu_name: The machine name of the menu.
  • items: A nested list of menu items. Each menu item contains:
    • attributes: HTML attributes for the menu item.
    • below: The menu item child items.
    • title: The menu link title.
    • url: The menu link url, instance of \Drupal\Core\Url
    • localized_options: Menu link localized options.
    • is_expanded: TRUE if the link has visible children within the current menu tree.
    • is_collapsed: TRUE if the link has children within the current menu tree that are not currently visible.
    • in_active_trail: TRUE if the link is in the active trail.

File

core/themes/olivero/templates/navigation/menu--primary-menu.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Olivero's theme implementation for the menus in the primary_menu region.
  5. *
  6. * Available variables:
  7. * - menu_name: The machine name of the menu.
  8. * - items: A nested list of menu items. Each menu item contains:
  9. * - attributes: HTML attributes for the menu item.
  10. * - below: The menu item child items.
  11. * - title: The menu link title.
  12. * - url: The menu link url, instance of \Drupal\Core\Url
  13. * - localized_options: Menu link localized options.
  14. * - is_expanded: TRUE if the link has visible children within the current
  15. * menu tree.
  16. * - is_collapsed: TRUE if the link has children within the current menu tree
  17. * that are not currently visible.
  18. * - in_active_trail: TRUE if the link is in the active trail.
  19. *
  20. * @ingroup themeable
  21. */
  22. #}
  23. {% import _self as menus %}
  24. {#
  25. We call a macro which calls itself to render the full tree.
  26. @see https://twig.symfony.com/doc/1.x/tags/macro.html
  27. #}
  28. {% set attributes = attributes.addClass('menu') %}
  29. {{ menus.menu_links(items, attributes, 0) }}
  30. {% macro menu_links(items, attributes, menu_level) %}
  31. {% set primary_nav_level = 'primary-nav__menu--level-' ~ (menu_level + 1) %}
  32. {% set drupal_selector_primary_nav_level = 'primary-nav-menu--level-' ~ (menu_level + 1) %}
  33. {% import _self as menus %}
  34. {% if items %}
  35. {#
  36. Place the menu arrow (caret) outside of the submenu because the submenu
  37. has the overflow:hidden CSS rule applied.
  38. #}
  39. {% if menu_level == 1 %}
  40. <span data-drupal-selector="primary-nav-menu-🥕" class="primary-nav__menu-🥕"></span>
  41. {% endif %}
  42. <ul {{ attributes.addClass('primary-nav__menu', primary_nav_level).setAttribute('data-drupal-selector', drupal_selector_primary_nav_level) }}>
  43. {% set attributes = attributes.removeClass(primary_nav_level) %}
  44. {% for item in items %}
  45. {% if item.url.isRouted and item.url.routeName == '<nolink>' %}
  46. {% set menu_item_type = 'nolink' %}
  47. {% elseif item.url.isRouted and item.url.routeName == '<button>' %}
  48. {% set menu_item_type = 'button' %}
  49. {% else %}
  50. {% set menu_item_type = 'link' %}
  51. {% endif %}
  52. {% set item_classes = [
  53. 'primary-nav__menu-item',
  54. 'primary-nav__menu-item--' ~ menu_item_type,
  55. 'primary-nav__menu-item--level-' ~ (menu_level + 1),
  56. item.in_active_trail ? 'primary-nav__menu-item--active-trail',
  57. item.below ? 'primary-nav__menu-item--has-children',
  58. ]
  59. %}
  60. {% set link_classes = [
  61. 'primary-nav__menu-link',
  62. 'primary-nav__menu-link--' ~ menu_item_type,
  63. 'primary-nav__menu-link--level-' ~ (menu_level + 1),
  64. item.in_active_trail ? 'primary-nav__menu-link--active-trail',
  65. item.below ? 'primary-nav__menu-link--has-children',
  66. ]
  67. %}
  68. <li{{ item.attributes.addClass(item_classes).setAttribute('data-drupal-selector', item.below ? 'primary-nav-menu-item-has-children' : false) }}>
  69. {#
  70. A unique HTML ID should be used, but that isn't available through
  71. Twig yet, so the |clean_id filter is used for now.
  72. @see https://www.drupal.org/project/drupal/issues/3115445
  73. #}
  74. {% set aria_id = (item.title ~ '-submenu-' ~ loop.index )|clean_id %}
  75. {% set link_title %}
  76. <span class="primary-nav__menu-link-inner">{{ item.title }}</span>
  77. {% endset %}
  78. {% if menu_item_type == 'link' or menu_item_type == 'nolink' %}
  79. {{ link(menu_item_type == 'link' ? link_title : item.title, item.url, {
  80. 'class': link_classes,
  81. 'data-drupal-selector': 'primary-nav-menu-link-has-children',
  82. })
  83. }}
  84. {% if item.below %}
  85. {#
  86. Aria-hidden and tabindex attributes are removed via JS. Button is non-functional,
  87. but still visible in non-JS environments so that chevron can indicate presence of
  88. drop menu).
  89. #}
  90. {%
  91. set toggle_button_attributes = create_attribute({
  92. 'class': 'primary-nav__button-toggle',
  93. 'data-drupal-selector': 'primary-nav-submenu-toggle-button',
  94. 'aria-controls': aria_id,
  95. 'aria-expanded': 'false',
  96. 'aria-hidden': 'true',
  97. 'tabindex': '-1',
  98. })
  99. %}
  100. <button{{ toggle_button_attributes }}>
  101. <span class="visually-hidden">{{ '@title sub-navigation'|t({'@title': item.title}) }}</span>
  102. <span class="icon--menu-toggle"></span>
  103. </button>
  104. {% set attributes = attributes.setAttribute('id', aria_id) %}
  105. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  106. {% endif %}
  107. {% elseif menu_item_type == 'button' %}
  108. {{ link(link_title, item.url, {
  109. 'class': link_classes,
  110. 'aria-controls': item.below ? aria_id : false,
  111. 'aria-expanded': item.below ? 'false' : false,
  112. 'data-drupal-selector': item.below ? 'primary-nav-submenu-toggle-button' : false,
  113. })
  114. }}
  115. {% set attributes = attributes.setAttribute('id', aria_id) %}
  116. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  117. {% endif %}
  118. </li>
  119. {% endfor %}
  120. </ul>
  121. {% endif %}
  122. {% endmacro %}

Related topics