I am looking to modify the default output of Drupal 8 of links in a certain situation, the language switcher. Ideally I want to be able to do that in a twig template and not have to produce a massive php file that overwwrites that whole link generation system and add a BIG if for the cases I need to overwrite.
I think the output layout should be controllable in the twig template similar to this answer (this answer outputs the same url for every languages)
I am using bootstrap as a base theme so i'd like to use the bootstrap markup for my links (btn btn-primary, dropdown)
Here is my code I am trying to make links--language-block.html.twig
{%- if links|length == 2 -%}
{# show only alternate language button #}
{%- for key, item in links -%}
{%- if not item.attributes['#options']['set_active_class'] -%} {# <--- this is always true!? #}
<a href="{{ item.link['#url'] }}" class="btn btn-primary">{{ item.link['#title'] }}</a> {# #url is always the same what ever the language it is pointing to! #}
{%- endif -%}
{%- endfor -%}
{%- elseif links|length > 2 -%}
{# show selected language in button and other languages in drop down #}
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{ selectedLanguage }}<span class="caret"></span></button>
<ul class="dropdown-menu">
{% for key, item in links %}
{%- if not item.attributes['#options']['set_active_class'] -%} {# <--- this is always true!? #}
<li><a href="{{ item.link['#url'] }}">{{ item.link['#title'] }}</a></li>
{% endif %}
{% endfor %}
</ul>
{%- endif -%}
Anyone has an idea on how to do this?