menu.html.twig

Same filename in this branch
  1. 11.x core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  2. 11.x core/themes/stable9/templates/navigation/menu.html.twig
  3. 11.x core/themes/claro/templates/classy/navigation/menu.html.twig
  4. 11.x core/themes/starterkit_theme/templates/navigation/menu.html.twig
  5. 11.x core/modules/system/templates/menu.html.twig
Same filename and directory in other branches
  1. 9 core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  2. 9 core/themes/olivero/templates/navigation/menu.html.twig
  3. 9 core/themes/stable9/templates/navigation/menu.html.twig
  4. 9 core/themes/seven/templates/classy/navigation/menu.html.twig
  5. 9 core/themes/claro/templates/classy/navigation/menu.html.twig
  6. 9 core/themes/bartik/templates/classy/navigation/menu.html.twig
  7. 9 core/themes/stable/templates/navigation/menu.html.twig
  8. 9 core/themes/starterkit_theme/templates/navigation/menu.html.twig
  9. 9 core/themes/classy/templates/navigation/menu.html.twig
  10. 9 core/modules/system/templates/menu.html.twig
  11. 8.9.x core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  12. 8.9.x core/themes/seven/templates/classy/navigation/menu.html.twig
  13. 8.9.x core/themes/claro/templates/classy/navigation/menu.html.twig
  14. 8.9.x core/themes/bartik/templates/classy/navigation/menu.html.twig
  15. 8.9.x core/themes/stable/templates/navigation/menu.html.twig
  16. 8.9.x core/themes/classy/templates/navigation/menu.html.twig
  17. 8.9.x core/modules/system/templates/menu.html.twig
  18. 10 core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  19. 10 core/themes/olivero/templates/navigation/menu.html.twig
  20. 10 core/themes/stable9/templates/navigation/menu.html.twig
  21. 10 core/themes/claro/templates/classy/navigation/menu.html.twig
  22. 10 core/themes/starterkit_theme/templates/navigation/menu.html.twig
  23. 10 core/modules/system/templates/menu.html.twig

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.
1 theme call to menu.html.twig
MenuLinkTreeTest::providerTestBuildCacheability in core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
Provides the test cases to test for ::testBuildCacheability().

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


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