I am trying to override the titles of the nodes on my view page for Drupal 8. As my whole website is in English, but only on one view page I need to fetch the french translation of the titles, without translating the whole interface.

For this I am overriding the views-view-field.html.twig template with custom views-view-field--my-view--title.html.twig.

In the preprocess function in MYTHEME.theme I am sending the language = 'fr':

function MYTHEME_preprocess_views_view_field__MYVIEW__title(&$variables) {
  $variables['language'] = 'fr';
  $variables['#cache']['contexts'][] = 'route';

The Twig template receives the language, but still displays titles in English. This is how my Twig template currently looks like. How can I say to fetch output in French?

{{ output }}


thanks to Kint sub-module I could get all possible tokens. Now I need to fetch the right translation.

How can I reach french translation? I have tried already few options:

{{ row._entity.title.value.translation('fr') }}
{{ row._entity.title.value.fr }}
{{ row._entity.values.title.translation('fr') }}
{{ row._entity.values.title.fr }}

None of them is working. As I can see the values is an array, then it should be some special may accesing the elements inside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy