menu--main.html.twig

Same filename in other branches
  1. 9 core/profiles/demo_umami/themes/umami/templates/components/navigation/menu--main.html.twig
  2. 8.9.x core/profiles/demo_umami/themes/umami/templates/components/navigation/menu--main.html.twig
  3. 10 core/profiles/demo_umami/themes/umami/templates/components/navigation/menu--main.html.twig

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

View source
  1. {#
  2. /**
  3. * @file
  4. * Theme override to display 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. #}
  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).setAttribute('data-drupal-selector', 'menu-main') }}> {# 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.