menu.html.twig

Same filename in this branch
  1. 10 core/themes/olivero/templates/navigation/menu.html.twig
  2. 10 core/themes/stable9/templates/navigation/menu.html.twig
  3. 10 core/themes/claro/templates/classy/navigation/menu.html.twig
  4. 10 core/themes/starterkit_theme/templates/navigation/menu.html.twig
  5. 10 core/modules/system/templates/menu.html.twig
Same filename 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. 11.x core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  19. 11.x core/themes/olivero/templates/navigation/menu.html.twig
  20. 11.x core/themes/stable9/templates/navigation/menu.html.twig
  21. 11.x core/themes/claro/templates/classy/navigation/menu.html.twig
  22. 11.x core/themes/starterkit_theme/templates/navigation/menu.html.twig
  23. 11.x core/modules/system/templates/menu.html.twig

Theme override to display a 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/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig

View source
  1. {#
  2. /**
  3. * @file
  4. * Theme override to display a 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. #}
  21. {% import _self as menus %}
  22. {#
  23. We call a macro which calls itself to render the full tree.
  24. @see https://twig.symfony.com/doc/3.x/tags/macro.html
  25. 1. We use menu_name (see above) to create a CSS class name from it.
  26. See https://www.drupal.org/node/2649076
  27. #}
  28. {{ menus.menu_links(items, attributes, 0, menu_name) }} {# 1. #}
  29. {% macro menu_links(items, attributes, menu_level, menu_name) %} {# 1. #}
  30. {% import _self as menus %}
  31. {# 1. #}
  32. {%
  33. set menu_classes = [
  34. 'menu-' ~ menu_name|clean_class,
  35. ]
  36. %}
  37. {# 1. #}
  38. {%
  39. set submenu_classes = [
  40. 'menu-' ~ menu_name|clean_class ~ '__submenu',
  41. ]
  42. %}
  43. {% if items %}
  44. {% if menu_level == 0 %}
  45. <ul{{ attributes.addClass(menu_classes) }}> {# 1. #}
  46. {% else %}
  47. <ul{{ attributes.removeClass(menu_classes).addClass(submenu_classes) }}> {# 1. #}
  48. {% endif %}
  49. {% for item in items %}
  50. {# 1. #}
  51. {%
  52. set item_classes = [
  53. 'menu-' ~ menu_name|clean_class ~ '__item',
  54. item.is_expanded ? 'menu-' ~ menu_name|clean_class ~ '__item--expanded',
  55. item.is_collapsed ? 'menu-' ~ menu_name|clean_class ~ '__item--collapsed',
  56. item.in_active_trail ? 'menu-' ~ menu_name|clean_class ~ '__item--active-trail',
  57. ]
  58. %}
  59. {# 1. #}
  60. {%
  61. set link_classes = [
  62. 'menu-' ~ menu_name|clean_class ~ '__link',
  63. ]
  64. %}
  65. <li{{ item.attributes.addClass(item_classes) }}>{# 1. #}
  66. {# 1. #}
  67. {{
  68. link(
  69. item.title,
  70. item.url,
  71. item.attributes.removeClass(item_classes).addClass(link_classes)
  72. )
  73. }}
  74. {% if item.below %}
  75. {{ menus.menu_links(item.below, attributes, menu_level + 1, menu_name) }} {# 1. #}
  76. {% endif %}
  77. </li>
  78. {% endfor %}
  79. </ul>
  80. {% endif %}
  81. {% endmacro %}

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