Skip to content

Instantly share code, notes, and snippets.

@4aficiona2
Last active February 8, 2023 21:38
Show Gist options
  • Save 4aficiona2/268c60aa5df4dc3b2e8e to your computer and use it in GitHub Desktop.
Save 4aficiona2/268c60aa5df4dc3b2e8e to your computer and use it in GitHub Desktop.
Drupal 8 - Main menu twig template with adjustments for Yokai, base template was initialy classy theme
{#
/**
* @file
* Default theme implementation to display a 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.
*
* @ingroup themeable
*/
#}
{% 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, menu_name) }}
{% macro menu_links(items, attributes, menu_level, menu_name) %}
{% import _self as menus %}
{%
set menu_classes = [
'menu',
'menu--' ~ menu_name,
]
%}
{% if items %}
{% if menu_level == 0 %}
<ul{{ attributes.addClass(menu_classes) }}>
{% else %}
<ul>
{% endif %}
{% for item in items %}
{#
project specific, not included in array because all top level items are expanded
item.is_expanded ? 'menu-item--expanded',
item.is_collapsed ? 'menu-item--collapsed',
#}
{%
set classes = [
'menu-item',
item.in_active_trail ? 'menu-item--active-trail',
]
%}
{#
project specific, custom rule which adds a highlighted class to the last toplevel menu item
#}
{% if (menu_level == 0) and (loop.last) %}
{% set classes = classes|merge(['menu-item--highlighted']) %}
{% endif %}
<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 %}
</ul>
{% endif %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment