You are here

menu.html.twig in Drupal 10

Olivero's theme implementation for the main menu.

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.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Olivero's theme implementation for the main menu.
  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/3.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 = 'menu--level-' ~ (menu_level + 1) %}
  32. {% import _self as menus %}
  33. {% if items %}
  34. <ul {{ attributes.addClass('menu', primary_nav_level) }}>
  35. {% set attributes = attributes.removeClass(primary_nav_level) %}
  36. {% for item in items %}
  37. {% if item.url.isRouted and item.url.routeName == '<nolink>' %}
  38. {% set menu_item_type = 'nolink' %}
  39. {% elseif item.url.isRouted and item.url.routeName == '<button>' %}
  40. {% set menu_item_type = 'button' %}
  41. {% else %}
  42. {% set menu_item_type = 'link' %}
  43. {% endif %}
  44. {% set item_classes = [
  45. 'menu__item',
  46. 'menu__item--' ~ menu_item_type,
  47. 'menu__item--level-' ~ (menu_level + 1),
  48. item.in_active_trail ? 'menu__item--active-trail',
  49. item.below ? 'menu__item--has-children',
  50. ]
  51. %}
  52. {% set link_classes = [
  53. 'menu__link',
  54. 'menu__link--' ~ menu_item_type,
  55. 'menu__link--level-' ~ (menu_level + 1),
  56. item.in_active_trail ? 'menu__link--active-trail',
  57. item.below ? 'menu__link--has-children',
  58. ]
  59. %}
  60. <li{{ item.attributes.addClass(item_classes) }}>
  61. {#
  62. A unique HTML ID should be used, but that isn't available through
  63. Twig yet, so the |clean_id filter is used for now.
  64. @see https://www.drupal.org/project/drupal/issues/3115445
  65. #}
  66. {% set aria_id = (item.title ~ '-submenu-' ~ loop.index )|clean_id %}
  67. {{ link(item.title, item.url, { 'class': link_classes }) }}
  68. {% if item.below %}
  69. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  70. {% endif %}
  71. </li>
  72. {% endfor %}
  73. </ul>
  74. {% endif %}
  75. {% endmacro %}

Related topics