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
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 %}

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon