diff --git a/components/nav_menu/nav_menu.twig b/components/nav_menu/nav_menu.twig index e6d7171f1fe25fb547e4f64da0e44ac4426b4af4..b80e3b158fab881d6ebb031480204ee4ee6f519e 100644 --- a/components/nav_menu/nav_menu.twig +++ b/components/nav_menu/nav_menu.twig @@ -14,7 +14,7 @@ {% macro menu_links(items, attributes, menu_level, menu_id) %} {% if items %} {% if menu_level == 0 %} - {% for item in items %} + {% for item in items %} {% if item.below or item.url %} <li{{ item.attributes.addClass('fr-nav__item') }}> {% if item.below %} @@ -29,21 +29,17 @@ {{ _self.menu_links(item.below, attributes, menu_level + 1) }} </div> {% else %} - <a class="fr-nav__link" - href="{{ item.url }}"{% if item.in_active_trail %} aria-current="page"{% endif %}> - {{ item.title }} - </a> + {{ _self.singlelink(item) }} {% endif %} </li> {% endif %} - {% endfor %} + {% endfor %} {% else %} <ul class="fr-menu__list"> {% for item in items %} {% if item.url %} <li{{ item.attributes.addClass('fr-nav__item') }}> - <a class="fr-nav__link" - href="{{ item.url }}"{% if item.in_active_trail %} aria-current="page"{% endif %}>{{ item.title }}</a> + {{ _self.singlelink(item) }} </li> {% endif %} {% endfor %} @@ -68,10 +64,7 @@ {{ _self.mega_menu_links(item.below, attributes, menu_level + 1, menu_id, item_id, item) }} </div> {% else %} - <a class="fr-nav__link" - href="{{ item.url }}"{% if item.in_active_trail %} aria-current="page"{% endif %}> - {{ item.title }} - </a> + {{ _self.singlelink(item) }} {% endif %} </li> {% endif %} @@ -106,10 +99,7 @@ {% if not item.url %} <span class="fr-nav__link">{{ item.title }}</span> {% else %} - <a class="fr-nav__link" - href="{{ item.url }}"{% if item.in_active_trail %} aria-current="page"{% endif %}> - {{ item.title }} - </a> + {{ _self.singlelink(item) }} {% endif %} </h5> {% if item.below %} @@ -125,8 +115,7 @@ {% for item in items %} {% if item.url %} <li{{ item.attributes }}> - <a class="fr-nav__link" - href="{{ item.url }}"{% if item.in_active_trail %} aria-current="page"{% endif %}>{{ item.title }}</a> + {{ _self.singlelink(item) }} </li> {% endif %} {% endfor %} @@ -134,3 +123,15 @@ {% endif %} {% endif %} {% endmacro %} + +{% macro singlelink(item) %} + {% set link_attributes = create_attribute(item.link_attributes|default({})).addClass('fr-nav__link') %} + {% set link_attributes = item.url ? link_attributes.setAttribute('href', item.url) : link_attributes %} + {% if link_attributes.target == '_blank' %} + {% set link_attributes = link_attributes.setAttribute('title', '@title - new window'|t({'@title': link_attributes.title ?: item.title})) %} + {% endif %} + {% if item.in_active_trail %} + {% set link_attributes = link_attributes.setAttribute('aria-current', 'page') %} + {% endif %} + <a{{ link_attributes }}>{{ item.title }}</a> +{% endmacro %}