You are here

menu--secondary-menu.html.twig in Drupal 9

Olivero's theme implementation for the menus in the secondary_menu region.

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/themes/olivero/templates/navigation/menu--secondary-menu.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Olivero's theme implementation for the menus in the secondary_menu region.
  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/1.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 secondary_nav_level = 'secondary-nav__menu--level-' ~ (menu_level + 1) %}
  32. {% import _self as menus %}
  33. {% if items %}
  34. <ul{{ attributes.addClass('secondary-nav__menu', secondary_nav_level) }}>
  35. {% set attributes = attributes.removeClass(secondary_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. 'secondary-nav__menu-item',
  46. 'secondary-nav__menu-item--' ~ menu_item_type,
  47. 'secondary-nav__menu-item--level-' ~ (menu_level + 1),
  48. item.in_active_trail ? 'secondary-nav__menu-item--active-trail',
  49. item.below ? 'secondary-nav__menu-item--has-children',
  50. ]
  51. %}
  52. {% set link_classes = [
  53. 'secondary-nav__menu-link',
  54. 'secondary-nav__menu-link--' ~ menu_item_type,
  55. 'secondary-nav__menu-link--level-' ~ (menu_level + 1),
  56. item.in_active_trail ? 'secondary-nav__menu-link--active-trail',
  57. item.below ? 'secondary-nav__menu-link--has-children',
  58. ]
  59. %}
  60. <li{{ item.attributes.addClass(item_classes) }}>
  61. {{ link(item.title, item.url, { 'class': link_classes }) }}
  62. {% if item.below %}
  63. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  64. {% endif %}
  65. </li>
  66. {% endfor %}
  67. </ul>
  68. {% endif %}
  69. {% endmacro %}

Related topics