Same filename in this branch
  1. 10 core/modules/system/templates/menu.html.twig
  2. 10 core/themes/olivero/templates/navigation/menu.html.twig
  3. 10 core/themes/stable9/templates/navigation/menu.html.twig
  4. 10 core/themes/starterkit_theme/templates/navigation/menu.html.twig
  5. 10 core/themes/claro/templates/classy/navigation/menu.html.twig
  6. 10 core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
Same filename and directory in other branches
  1. 8.9.x core/modules/system/templates/menu.html.twig
  2. 9 core/modules/system/templates/menu.html.twig

Default theme implementation 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/modules/system/templates/menu.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Default theme implementation 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. * @ingroup themeable
  21. */
  22. #}
  23. {% import _self as menus %}
  24. {#
  25. We call a macro which calls itself to render the full tree.
  26. https://twig.symfony.com/doc/3.x/tags/macro.html
  27. #}
  28. {{ menus.menu_links(items, attributes, 0) }}
  29. {% macro menu_links(items, attributes, menu_level) %}
  30. {% import _self as menus %}
  31. {% if items %}
  32. {% if menu_level == 0 %}
  33. <ul{{ attributes }}>
  34. {% else %}
  35. <ul>
  36. {% endif %}
  37. {% for item in items %}
  38. <li{{ item.attributes }}>
  39. {{ link(item.title, item.url) }}
  40. {% if item.below %}
  41. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  42. {% endif %}
  43. </li>
  44. {% endfor %}
  45. </ul>
  46. {% endif %}
  47. {% endmacro %}

Related topics