menu.html.twig

Same filename in this branch
  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/classy/templates/navigation/menu.html.twig
  9. 9 core/modules/system/templates/menu.html.twig
Same filename in other branches
  1. 8.9.x core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  2. 8.9.x core/themes/seven/templates/classy/navigation/menu.html.twig
  3. 8.9.x core/themes/claro/templates/classy/navigation/menu.html.twig
  4. 8.9.x core/themes/bartik/templates/classy/navigation/menu.html.twig
  5. 8.9.x core/themes/stable/templates/navigation/menu.html.twig
  6. 8.9.x core/themes/classy/templates/navigation/menu.html.twig
  7. 8.9.x core/modules/system/templates/menu.html.twig
  8. 10 core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  9. 10 core/themes/olivero/templates/navigation/menu.html.twig
  10. 10 core/themes/stable9/templates/navigation/menu.html.twig
  11. 10 core/themes/claro/templates/classy/navigation/menu.html.twig
  12. 10 core/themes/starterkit_theme/templates/navigation/menu.html.twig
  13. 10 core/modules/system/templates/menu.html.twig
  14. 11.x core/profiles/demo_umami/themes/umami/templates/components/navigation/menu.html.twig
  15. 11.x core/themes/olivero/templates/navigation/menu.html.twig
  16. 11.x core/themes/stable9/templates/navigation/menu.html.twig
  17. 11.x core/themes/claro/templates/classy/navigation/menu.html.twig
  18. 11.x core/themes/starterkit_theme/templates/navigation/menu.html.twig
  19. 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/themes/starterkit_theme/templates/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/1.x/tags/macro.html
  25. #}
  26. {{ menus.menu_links(items, attributes, 0) }}
  27. {% macro menu_links(items, attributes, menu_level) %}
  28. {% import _self as menus %}
  29. {% if items %}
  30. {% if menu_level == 0 %}
  31. <ul{{ attributes.addClass('menu') }}>
  32. {% else %}
  33. <ul class="menu">
  34. {% endif %}
  35. {% for item in items %}
  36. {%
  37. set classes = [
  38. 'menu-item',
  39. item.is_expanded ? 'menu-item--expanded',
  40. item.is_collapsed ? 'menu-item--collapsed',
  41. item.in_active_trail ? 'menu-item--active-trail',
  42. ]
  43. %}
  44. <li{{ item.attributes.addClass(classes) }}>
  45. {{ link(item.title, item.url) }}
  46. {% if item.below %}
  47. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  48. {% endif %}
  49. </li>
  50. {% endfor %}
  51. </ul>
  52. {% endif %}
  53. {% endmacro %}

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