Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phunguyen19/fc2e66ed7fadd0b6fcb00242053f4ecf to your computer and use it in GitHub Desktop.
Save phunguyen19/fc2e66ed7fadd0b6fcb00242053f4ecf to your computer and use it in GitHub Desktop.
drupal 8, display node field reference entity (label and url)
{#
/**
* This snippet works with Drupal 8.3.x
*
* Template for using: node.html.twig.
*
* This code below works with conditions:
* - templates for using: node.html.twig and node convention templates
* - field is a reference to taxonomy_term entity name : "category"
* - field machine name "field_category"
* - field display format is "Rendered entity"
*/
#}
{% for i, value in content.field_category %}
{% if node.field_category[i].entity %}
{%
set cat = {
url : path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_category[i].entity.id}),
title : node.field_category[i].entity.name.value,
}
%}
<a href="{{ cat.url }}">{{ cat.title }}</a>
{% endif %}
{% endfor %}
@GuitarKat
Copy link

Thanks! This also works for any generic nodes, including teaser view modes as long as you are rendering the node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment