You are here

smartmenus-menu.html.twig in Smartmenus.js 8

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

templates/smartmenus-menu.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 smartmenus %}
  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. 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. <nav class="main-nav smartmenus-nav" role="navigation">
  29. {{ toggle }}
  30. {{ smartmenus.menu_links(items, attributes, 0, menu_name) }} {# 1. #}
  31. </nav>
  32. {% macro menu_links(items, attributes, menu_level, menu_name) %} {# 1. #}
  33. {% import _self as smartmenus %}
  34. {# 1. #}
  35. {%
  36. set menu_classes = [
  37. 'sm sm-' ~ menu_name|clean_class,
  38. ]
  39. %}
  40. {%
  41. set submenu_classes = [
  42. 'sm-submenu',
  43. ]
  44. %}
  45. {% if items %}
  46. {% if menu_level == 0 %}
  47. <ul id="sm-main-menu" {{ attributes.addClass(menu_classes).setAttribute('data-drupal-selector', 'smartmenu') }}> {# 1. #}
  48. {% else %}
  49. <ul {{ attributes.addClass(submenu_classes) }}>
  50. {% endif %}
  51. {% for item in items %}
  52. {%
  53. set item_classes = [
  54. 'sm-item',
  55. item.is_expanded ? 'item-expanded',
  56. item.is_collapsed ? 'item-collapsed',
  57. item.in_active_trail ? 'item-active-trail',
  58. loop.first and menu_level == 0 ? 'first',
  59. loop.last and menu_level == 0 ? 'last'
  60. ]
  61. %}
  62. {%
  63. set link_classes = [
  64. 'sm-link',
  65. ]
  66. %}
  67. <li{{ item.attributes.addClass(item_classes) }}>
  68. {{
  69. link(
  70. item.title,
  71. item.url,
  72. item.attributes.removeClass(item_classes).addClass(link_classes)
  73. )
  74. }}
  75. {% if item.below %}
  76. {{ smartmenus.menu_links(item.below, attributes, menu_level + 1, menu_name) }}
  77. {% endif %}
  78. </li>
  79. {% endfor %}
  80. </ul>
  81. {% endif %}
  82. {% endmacro %}