Get referenced node fields in twig In Drupal 8, I have a content type, that has an "entity_reference" field and I wish to access the referenced node's fields in twig template.
the kint() output for the field is:


In this list I can't find the referenced node's field values. How can I (if at all possible) load the node or node fields/values in twig?

Best,

Alari
The referenced nodes are inside the list and you can get the first list item with .0, the referenced node with .entity and then the first field value:

For example if you are in a page or node template:

{{ node.field_reference.0.entity.field_in_referenced_node.0.value }}


You can ommit .0 in both cases, when you only want to get the first value.

Other examples could look like this, but your question is unclear exactly where you are:

{{ item.content['#item'].entity.field_in_referenced_node.value }}
{{ elements['#items'].0.entity.field_in_referenced_node.value }}


Edit: Counting the items in multivalue fields

{{ node.field_reference.getvalue|length }}


You can't use count() in twig, because this is not on the whitelist for allowed methods. This uses getvalue() to get the array of the field and then counts the array keys.