Skip to main content
Category:

Code snippet that can help you print entity reference values in twig template in drupal 8.

Example 1 - limited field:

{{ node.field_REFERENCE_NODE.entity.field_name.value }}

Example 2 - unlimited field:

{% for key,value in node.field_REFERENCE_NODE.value %}
  {#  link to reference node #}
  <a href="{{ path('entity.node.canonical', { 'node' : content.field_REFERENCE_NODE[key]['#node'].nid.value}) }}">{{ 'read more'|t }}</a>
  {#  get field image and alt #}
  <img src="{{ file_url(content.field_REFERENCE_NODE[key]['#node'].field_image.entity.uri.value) }}"
       alt="{{ content.field_REFERENCE_NODE[key]['#node'].field_image.alt }}"/>
  {#  get title#}
  <p>{{ content.field_REFERENCE_NODE[key]['#node'].getTitle() }}</p>

{% endfor %}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook