How can I add custom classes for links into main menu in Drupal 8. This is template for my main menu called menu--main.html.twig
{% import _self as menus %}
{{ 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('main-nav') }}>
{% for item in items %}
<li{{ item.attributes.addClass('main-nav__item') }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
{% else %}
<ul class="main-nav__sub js-nav-submenu ">
{% for item in items %}
<li{{ item.attributes.addClass('main-nav__sub-item') }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
{% endif %}
</ul>
{% endif %}
{% endmacro %}
And I need to achieve this result:
<ul class="main-nav">
<li class="main-nav__item"><a class="main-nav__link main-nav__link--active" href="index.html">Úvod</a></li>
<li class="main-nav__item"><a class="main-nav__link js-nav-submenu-toggler" href="#">O škole</a>
<ul class="main-nav__sub js-nav-submenu">
<li class="main-nav__sub-item"><a class="main-nav__sub-link" href="#">Aktuality</a></li>
<li class="main-nav__sub-item"><a class="main-nav__sub-link " href="pedagogovia.html">Pedagógovia</a></li>
<li class="main-nav__sub-item"><a class="main-nav__sub-link" href="#">Dokumenty</a></li>
<li class="main-nav__sub-item"><a class="main-nav__sub-link " href="historia.html">História</a></li>
</ul>
</li>
<li class="main-nav__item"><a class="main-nav__link" href="#">Informácie o štúdiu</a></li>
<li class="main-nav__item"><a class="main-nav__link" href="#">Fotogaléria</a></li>
<li class="main-nav__item"><a class="main-nav__link " href="kontakt.html">Kontakt</a></li>
</ul>
My problem is that I don't know how to set custom class for <a href="">
tags. Is there any way how can do it using link()
function. In Drupal 7 is l()
function but attributes aren't similar. l()
has also third attribute with possibility to add options. Is it true, that link($title, $url)
accepts only 2 parameters? Because I can't find the way how to send options for link($title, $url)
. I don't insit on link()
function, so if there is some better way please let me know. I'm open to new ideas :)
Thanks