9

I have a content type (Actuality) with an entity reference field to another content type (Event). In the referenced content type, I have a field_date field I want to print in the node--actuality.html.twig template file. I tried using {{ node.field_event_associated.field_date }}, but it doesn't work.

How can I print the field value of a referenced entity?

1

4 Answers 4

15

With a field_event_associated node field, you can get its value with {{ node.field_event_associated.value }}. That doesn't work for entity reference fields, for which you need to use {{ node.field_event_associated.entity.field_date.value }}.

5
  • Hi and thanks for your answer. I don't manage to access my values like this, the only thing i can do is : {{ content.field_evenement_associe[0]['#url'].options['entity'].field_date_event.value }} but i find it very complicated. Do i miss something ?
    – lchabrand
    Dec 12, 2016 at 5:59
  • 1
    If you apply the example from this answer you get a shorter version: {{ node.field_event_associated.entity.field_date.value }}.
    – 4uk4
    Dec 12, 2016 at 7:59
  • Thanks i get some results with this version. By the way, where can i find this in Drupal doc ? Regards
    – lchabrand
    Dec 12, 2016 at 23:48
  • You can find it in twig template documentation of drupal 8. Dec 13, 2016 at 13:24
  • It will show you the first value, How will get the All value available in entity reference fields Jun 21, 2019 at 14:20
8

If you want to render a properly formatted single field of a referenced entity you'll simply have to use the |view filter on the desired field of the referenced node.

Let's say the field_customer on my parent node references a customer content type entity that holds the customer's address in field_address.

{{ node.field_customer.entity.field_address|view }}

Use the referenced entity's display settings to define the format of the field you want to render.

Example on drupal.org

1

Output entity reference field value and create url on twig.

entity reference value : {{ node.field_name.entity.title.value }}

entity reference url : {{ url('entity.node.canonical', {'node': node.field_name.target_id}) }}

0

The value you're mostly looking for would be the title value:

{{ node.field_organisation_reference.entity.title.value }}

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.