menu--header.html.twig 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. {# this does not work because of the '@' #}
  46. {# we have to do it via js | not working neither #}
  47. {#{{ link(item.title, item.url, {'@click.prevent':['onclick']}) }}#}
  48. <a href="{{ item.url }}" @click.prevent='onclick'>{{ item.title }}</a>
  49. {% if item.below %}
  50. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  51. {% endif %}
  52. </li>
  53. {% endfor %}
  54. </ul>
  55. {% endif %}
  56. {% endmacro %}