I have a reference field for nodes and I have a field template to render the the fields.
{{ item.entity.title.value }}
But I can't get the translated title of the node.
Can someone help?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have a reference field for nodes and I have a field template to render the the fields.
{{ item.entity.title.value }}
But I can't get the translated title of the node.
Can someone help?
If you configure the reference field to display the entity title it should be translated automatically, as @leymannx commented. But if you get the title from the entity directly you have to make sure the entity is in the correct language. If you don't want to use PHP you can do this with the help of Twig Tweak:
{{ item.entity|translation.title.value }}
See https://git.drupalcode.org/project/twig_tweak/-/blob/3.x/docs/cheat-sheet.md#entity-translation
{{ item.entity.translation(lang).title.value }}
and {{ item.entity|translation.title.value }}
and other combinations. But always get an WSOD page with an error message. We use twig tweak and for all other things we use it works perfect, but thats the only problem we got stuck with.
May 11, 2021 at 7:11
{% for item in element['#items'] %}
. Do you have the latest version of Twig Tweak? |translation
was added quite recently.