n Drupal 8 - print entity reference values in twig template | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

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


Page Facebook