Hello Community,

since I wanted to show the output of a view in a custom DOM I created a custom views display style plugin via the following documentation
https://www.drupal.org/docs/8/creating-custom-modules/building-a-views-d...

Everything went quite smoothly till I recognized, that my multilingual page always shows the default language of the node in my view even when changing the language.

After some searching around I found out, that I have to manually check the current language in the TWIG template and get the translated node. That was quite the surprise for me since every other overwritten TWIG file in my custom theme didn't need that.

Im talking about the following:

  {% if rows|length %}

      {% for row in view.result %}

          {% set current_language = row.node_field_data_langcode %}
          {% if row._entity.hastranslation(current_language) %}
            {% set entity = row._entity.translation(current_language) %}
          {% else %}
            {# No translation available for this node #}
          {% endif %}

      {% endfor %}

  {% elseif empty %}

    <div class="view-empty">
      {{ empty }}
    </div>

  {% endif %}

And I just don't understand why this is necessary. Can't Drupal just set the node to the correct language before I iterate trough it in my custom views display style plugin?

Comments

kevin.pfeifer created an issue. See original summary.

kevin.pfeifer’s picture

Title: Current active language is not being detected in custom views display style plugin » View result in custom views display style plugin isn't set to current active language

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kevin.pfeifer’s picture

Status: Active » Closed (outdated)