Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Langswitcher Plugin broken?

Solved by Harald Schneider View solution

Started by Harald Schneider 6 years ago · 1 replies · 595 views
6 years ago

I installed the Langswitcher Plugin, but it looks like the langswitcher.language variable is not exposed to Twig. I cleaned the cache, checked Process Twig and Process Twig First with no result.

I use this Twit Partitial:

TWIG
<ul class="langswitcher"> DBG:
{% for language in langswitcher.language %}
    {% set show_language = true %}
    {% if language == langswitcher.current %}
        {% set lang_url = page.url %}
        {% set active_class = ' active' %}
    {% else %}
        {% set base_lang_url = base_url_simple ~ grav.language.getLanguageURLPrefix(language) %}
        {% set lang_url = base_lang_url ~ langswitcher.page_route ~ page.urlExtension %}
        {% set untranslated_pages_behavior = grav.config.plugins.langswitcher.untranslated_pages_behavior %}
        {% if untranslated_pages_behavior != 'none' %}
            {% set translated_page = langswitcher.translated_pages[language] %}
            {% if (not translated_page) or (not translated_page.published) %}
                {% if untranslated_pages_behavior == 'redirect' %}
                    {% set lang_url = base_lang_url ~ '/' %}
                {% elseif untranslated_pages_behavior == 'hide' %}
                    {% set show_language = false %}
                {% endif %}
            {% endif %}
        {% endif %}
        {% set active_class = '' %}
    {% endif %}

    {% if show_language %}
        <li><a href="{{ lang_url ~ uri.params }}" class="external{{ active_class }}">{{ native_name(language)|capitalize }}</a></li>
    {% endif %}

{% endfor %}
</ul>

The only output I get is

HTML
<ul class="langswitcher">DBG:</ul>

So the partitial is executed but its for loop is not entered.

I also checked the Plugin's PHP code with the debugger. It correctly collects the installed languages and assigns it to the Twig variable.

It looks like this info gets lost in the Twig Partitital itself.

Any hints ?

6 years ago Solution

Solved it 🙂

It depends on where

{% include 'partials/langswitcher.html.twig' %}

is added. For the Quark theme, this means:

  • If you put it into macros/macros.html.twig, it won't work because the language array in the twig variable is empty.
  • If you put it into navigation.html.twig, it is just the same.
  • If you use base.html.twig instead, it works like a charm.

This applies to both language plugins: Langswitcher and Language Selector Plugin.
I think it is an issue of multiple Twig parsing, because one file includes another ....

-- Harald

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 72 2 months ago