0

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?

3
  • Normally the title should be switched automatically depending on the currently selected interface language. Where and how do you want to use it? Was the node already translated or how else was the title translated? – leymannx May 10 at 18:57
  • The node was translated. And thats what I thought. That Drupal is smart enough ;) ... to get the translation. – Alex Misgin May 11 at 6:58
  • I use it in an Node as a reference. To follow the layout we had to use a field template. – Alex Misgin May 11 at 7:13
1

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

4
  • I tried {{ 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. – Alex Misgin May 11 at 7:11
  • The second version with a pipe symbol for the Twig filter seems to be correct if you iterate field items {% for item in element['#items'] %}. Do you have the latest version of Twig Tweak? |translation was added quite recently. – 4k4 May 11 at 8:00
  • I have Version Version: 8.x-2.9. – Alex Misgin May 11 at 8:59
  • OK now it works!!! THANK YOU SO MUCH for this simple solution to update!!!!!!! – Alex Misgin May 11 at 9:05

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.