Skip to main content
PK's Diary

Main navigation

  • Home
  • Reposts
  • Stories
  • Articles
  • Notes
  • About

Breadcrumb

  1. Home
  2. Notes

Accessing Translated Entity in Views Twig | Drupal 8

Drupal 8 Views Twig Entity Translation

Though ideally if we use the views correctly and override them well, Drupal Views itself take care of getting the translated version as per configurations of the view. For details check this: EntityField::getValue

But if somehow we wanna get translated version of the entity in a views twig, we will have to get the current language and then call ContentEntityBase::getTranslation on the entity of the views row. And there are various ways of getting the current language in the views-view.html.twig:

#1 Use preprocess to add current language

function MYTHEME_preprocess_views_view(&$vars) {

  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $vars['language'] = $language;
}

#2 Use Bamboo Twig module:

{% set language = bamboo_i18n_current_lang() %}

And then call getTranslation on the entity, like:

row.content['#row']._entity.getTranslation(language)

Additionally, you can add a check of hasTranslation as well​​​​​, if needed:

{% if row.content['#row']._entity.hastranslation(language) %}

This will do the job for you, but as recommended we should override twigs in a manner that we make use of Drupal Views way of getting entity field values which are translated based on what you configure in your views, if you remember views allow you to configure rendering language of current row. Overriding this way basically neglects those settings and renders in active language.

Apart from this method, there is also another way of doing it, which is just via preprocessing, but this might become complicated, as we will have to send array of translated results / rows of view separately:

$entity = \Drupal::service('entity.repository')->getTranslationFromContext($row->_entity);

Tags

  • Drupal
  • Drupal Planet
  • Twig
References
https://drupal.stackexchange.com/questions/248619/how-to-load-translated-entity…
  • Log in to post comments

Comments

Explore topics

TwigQED42Acquia CertificationDrupal PlanetDrupal CertificationThinkingDrupalOpen SourceLifeBackend Specialist
more tags

Quick Notes

Accessing Translated Entity in Views Twig | Drupal 8
Making a variable available (as global) in all twig files in Drupal
Data Structures and Algorithms - Solved in Javascript | Geeksforgeeks
Create alias for frequently used commands
Acquia Front-End Specialist Exam Preparation Pointers

Recent Stories around me

Software Engineer Interview Experience with Glovo
Why Amazon is better than Flipkart - Indian ecommerce market
Acquia Certified Drupal 8 Grand Master
Seminar at DCRUST on Open Source as Career
A Walk to Remember | June 2015 - August 2018 | QED42

Recent write-ups | PK's pen

How to check if Memcache, opcache or varnish is functional with the Drupal 8 application?
Getting started with ReactJS & Drupal (Fully-Decoupled)
Extending Drupal Translations for Custom Entity in Drupal 7
Google Assistant Integration with Drupal
Art of writing template files - Drupal 8
PK's Diary
Powered by Drupal