- 🇮🇳India gawalin
Below code worked for me.
{{ item.url.getOption('container_attributes').class[0] }}
If you create a custom twig template for the menu link and change the output of the menu from
{{ link(item.title, item.url) }}
to e.g.
<a href="{{ item.url }}" "{{ item.attributes.addClass(link_classes).removeClass(item_classes) }}" data-target="#" ><span class="main-menu__link-text">{{ item.title }}</span></a>
the menu attributes that were added through the menu link setting in the backend [0] are not rendered.
I am aware that this is caused by not using {{ link(item.title, item.url) }}
but here I need some custom classes which is why I went the a "href..." route.
Any idea of how I could get them into my "a href"? Thanks a lot for this module and your help! This is very much appreciated!
[0]:
Below I have added the menu.html.twig code
{#
/**
* @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 templates
*/
#}
{% 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 %}
<ul{{ attributes.addClass('menu', 'main-menu__list') }}>
{% else %}
<ul{{ attributes.addClass('dropdown-menu') }}>
{% endif %}
{% for item in items %}
{%
set item_classes = [
'main-menu__list-item',
item.in_active_trail ? 'active',
]
%}
{%
set link_classes = [
'main-menu__link-item',
item.title|clean_class,
item.in_active_trail ? 'is-active',
]
%}
{% if menu_level == 0 and item.is_expanded %}
<li{{ item.attributes.addClass(item_classes) }}>
<a href="{{ item.url }}" class="dropdown-toggle {{ item.url }}" data-target="#" data-toggle="dropdown">{{ item.title }} <span class="caret"></span></a>
{% else %}
<li{{ item.attributes.addClass(item_classes) }}>
<a href="{{ item.url }}" "{{ item.attributes.addClass(link_classes).removeClass(item_classes) }}" data-target="#" ><span class="main-menu__link-text">{{ item.title }}</span></a>
{# {{ link(item.title, item.url) }} #}
{% endif %}
{% if item.below %}
{{ menus.menu_links(item.below, attributes.removeClass('nav', 'navbar-nav'), menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
Active
1.0
Code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
Below code worked for me.
{{ item.url.getOption('container_attributes').class[0] }}