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, 2021 at 18:57
  • The node was translated. And thats what I thought. That Drupal is smart enough ;) ... to get the translation. May 11, 2021 at 6:58
  • I use it in an Node as a reference. To follow the layout we had to use a field template. May 11, 2021 at 7:13

1 Answer 1

3

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. May 11, 2021 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.
    – 4uk4
    May 11, 2021 at 8:00
  • I have Version Version: 8.x-2.9. May 11, 2021 at 8:59
  • OK now it works!!! THANK YOU SO MUCH for this simple solution to update!!!!!!! May 11, 2021 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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