0

How can I get access for the english values of my entity in the french version of my twig template. This is for a tracking task.

Let say I want to print the node title en english on the french version.

I was hoping I could do something like this : {{ node.languages.en.label }} but this doesn't work.

I'd rather avoid using preprocess if possible (I know how to do it with preprocess).

2
  • Stop thinking in D7 templates and start using hook_preprocess.
    – user21641
    Oct 23, 2017 at 10:18
  • Well I do already, but actually I'm not thinking in D7 templates but in Twig templates meaning using the less hook_preprocess possible for the frontend developer to have more freedom on their twig templating. I have 30 templates to edit with various values to get so...
    – pbonnefoi
    Oct 23, 2017 at 10:23

1 Answer 1

4

You can get the translated node with getTranslation():

{% if node.hastranslation('en') %}
  <p>{{ node.translation('en').title.value }}</p>
{% else %}
  <p>No translation<p/>
{% endif %}

If you are not sure that the translation exists, check first with hasTranslation(), because a missing translation throws an exception.

2
  • Well thank you very much, this was exactly what I needed. BTW, {{ node.translation('en').label }} works fine too in my case :-)
    – pbonnefoi
    Oct 23, 2017 at 10:30
  • Yes, I've used the title field for the example, because the question was about entity values. But you can use a method like label() on the translated node, too.
    – 4uk4
    Oct 23, 2017 at 11:11

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.