bs_base-8.x-1.x-dev/templates/navigation/menu--navbar.html.twig
templates/navigation/menu--navbar.html.twig
{#
/**
* @file
* Theme override 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.
* - 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.
*/
#}
{{ attach_library('bs_base/navbar') }}
{% if navbar_type %}
{{ attach_library('bs_base/navbar-' ~ navbar_type) }}
{% endif %}
{% if navbar_offcanvas_type %}
{{ attach_library('bs_lib/jasny-bootstrap-offcanvas') }}
{{ attach_library('bs_base/navbar-offcanvas') }}
{% endif -%}
{# @todo - consider maybe extending bs_base/menu.html.twig and then just change
what is needed. For now we are just adding navbar-nav. But we will probably
add support for navbar-dropdown. #}
{% 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(menu_name, items, attributes, 0, navbar_type, '') }}
{% macro menu_links(menu_name, items, attributes, menu_level, navbar_type, parent_id = '') %}
{% import _self as menus %}
{% if parent_id is empty %}
{% set parent_id = ('menu-' ~ menu_name)|clean_unique_id %}
{% endif %}
{%- if items -%}
{% if menu_level == 0 %}
<ul{{ attributes.addClass(['nav', 'navbar-nav']) }}>
{%- else -%}
<ul {{ navbar_type ? 'class="dropdown-menu"' }} aria-labelledby="{{ parent_id }}">
{%- endif -%}
{%- for item in items -%}
{% set aria_id = parent_id ~ '-' ~ loop.index0 %}
{%
set classes = [
'nav-item',
item.is_expanded and navbar_type ? 'dropdown',
item.in_active_trail ? 'active',
item.is_collapsed ? 'nav-item--collapsed',
]
%}
<li{{ item.attributes.addClass(classes) }}>
{#- We are using here also is-active because in one special case menu
item in_active_trail will not work correctly - when parent item and
child item are pointing to the same resource. In this case
in_active_trail for a child item will be false but item will still
have is-active class. Maybe this is a core bug? -#}
{#- Notice that we are adding nav-link only if item is visually not a
button. -#}
{% set link_attributes = {
'class': [
'btn' not in item.url.options.attributes.class ? 'nav-link',
menu_level > 0 and navbar_type ? 'dropdown-item',
item.in_active_trail ? 'active is-active',
],
'id': aria_id,
'data-menu-level': menu_level,
} %}
{% if item.is_expanded and 'no-link' not in item.url.options.attributes.class %}
{# We support only 3 level of depth and aria-expanded should be set
to true only for first two levels. For third level we will set
it to false so dropdown on active item can work. This is why
we have menu_level < 1 condition. #}
{% set link_attributes = link_attributes|merge({'aria-haspopup': 'true', 'aria-expanded': (item.in_active_trail and menu_level < 1) ? 'true' : 'false'}) %}
{% if navbar_type %}
{% set link_attributes = link_attributes|merge({'data-bs-toggle': 'dropdown'}) %}
{% endif %}
{# We will add dropdown-toggle icon as a span element so we can have
::after pseudo element on link free for other stuff. Space inside
of span we need because of BS .dropdown-toggle:empty::after
margin reset. #}
{% set item_title_class = 'label' ~ (navbar_type ? ' dropdown-toggle') %}
{% else %}
{% set item_title_class = 'label' %}
{% endif %}
{# When menu item URL has a no-link attribute we will render this item as
a plain text. #}
{%- if 'no-link' in item.url.options.attributes.class -%}
<div {{ create_attribute(link_attributes).addClass(item.url.options.attributes.class) }}><span class="{{ item_title_class }}">{{ item.title }}</span></div>
{%- else -%}
{{- link({'#markup': '<span class="' ~ item_title_class ~ '">' ~ item.title ~ '</span>'}, item.url, link_attributes) -}}
{%- endif -%}
{# Support item.content rendering from drupal/menu_item_extras module. #}
{% if item.content %}
{{ item.content }}
{%- elseif item.below -%}
{{- menus.menu_links(menu_name, item.below, attributes, menu_level + 1, navbar_type, aria_id ) -}}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{%- endif -%}
{% endmacro %}
