Skip to content

Instantly share code, notes, and snippets.

@ankedsgn
Last active August 16, 2021 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankedsgn/bad4d0f34ebc327cdca487ce787ddf87 to your computer and use it in GitHub Desktop.
Save ankedsgn/bad4d0f34ebc327cdca487ce787ddf87 to your computer and use it in GitHub Desktop.
Menu tpl that shows menu level (Drupal 8)
{% import _self as menus %}
{#
We call a macro which calls itself to render the full tree.
@see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0) }}
{% macro menu_links(items, attributes, menu_level) %}
{% import _self as menus %}
{% if items %}
{% if menu_level == 0 %}
<div class="navigation__main">
<ul{{ attributes }}>
{% else %}
<ul class="navigation__submenu menu-level-{{ menu_level }}">
{% endif %}
{% for item in items %}
{%
set classes = [
'menu-item',
item.is_expanded ? 'menu-item--expanded',
item.is_collapsed ? 'menu-item--collapsed',
item.in_active_trail ? 'menu-item--active-trail',
]
%}
<li{{ item.attributes.addClass(classes) }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
{% if menu_level == 0 %}
</ul>
</div>
{% else %}
</ul>
{% endif %}
{% endif %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment