1

i am trying to get row title -node title- in my custom module with template_preprocess_modulename_view($variables) and views-modulename-view.html.twig with using

row.content['#row']._entity.title.value

but it always return node title of translation source.

how can i get correct node translation title in twig?

--

update:

i am going to clarify my problem:

i have a view which returns nodes without any condition- example of returned output is like:

- 'title of node 1 in english'
- 'title of node 1 in french'

i want to manipulate title in twig but row.content['#row']._entity.title.value returns always node 1 in translation source language. -in this case english-

is it possible to get exact title of row -considering translation and language of returned row- in twig?

update 2

in template_preprocess_modulename_view($variables) i tried:

  $rows = $variables['rows'];

  foreach($rows as $index=>$value){
    if (isset($value['#row'])){
      $row=$value['#row'];
      var_dump($row->_entity->get('langcode')->value);
    }
  }

but $row->_entity->get('langcode')->value returns language of translation source of row, not language of row itself.

  • Possible duplicate of How to load translated entity values in Twig for SEO purpose? – leymannx Jul 18 at 19:43
  • 1
    @leymannx thanks but it uses node.hastranslation('en') but i don't know what is language of that row - i didn't filter a special language in that view- – user780 Jul 18 at 19:47
  • 2
    You can add $variables['current_language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();in preprocess and use current_language in place of 'en' – Clive Jul 18 at 19:52
  • @Clive is it possible to get language of row in template_preprocess_modulename_view($variables) with something like $rows = $variables['rows']; then looping and get language from $row['content']['#row']? – user780 Jul 18 at 20:07
  • 1
    If it's a view can't you just add it as a field and make it available that way? – Clive Jul 18 at 20:23
2

Normally you filter rows having all the same language, but you don't have to. So rows can have different languages.

The code in the question seems to get the entity from the wrong place. See this topic for examples of different kind of rows: How to access raw data in twig template using views table format

A normal views row is a ResultRow object having the property _entity, which then should return the translated entity:

$entity = $row->_entity;

See ResultRow::$_entity

But it doesn't. Tested it with the pre-installed view Content and I had to get the entity translation of the row language stored in node_field_data_langcode before I could get the translated title:

foreach (views_get_view_result('content') as $row) {
  $entity = $row->_entity;
  $langcode = $row->node_field_data_langcode;
  $entity = $entity->getTranslation($langcode);
  $translated_title = $entity->label();
}
  • thanks. i tried and i updated my question again. – user780 Jul 19 at 19:14
  • @user780 – $row->_entity->get('langcode')->value probably always returns the source language. But what does $entity = \Drupal::service('entity.repository')->getTranslationFromContext($row->_entity); get you? – leymannx Jul 19 at 19:22
  • 2
    getTranslationFromContext() gets the translation from the request context, not from the views row. Try to get the row from somewhere else. Can't help you with that because I don't know this hook: template_preprocess_modulename_view – 4k4 Jul 19 at 19:52
  • 1
    I wouldn't dig into row.content, because this is a render array and everything you find there can change without notice. A render array is not an API, it is only required to have valid render elements in no particular order. Use the View object to get the rows: $rows = $variables['view']->result; – 4k4 Jul 20 at 18:07
  • 1
    In Twig view.result, see the question I've linked in the answer. – 4k4 Jul 22 at 19:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.