Last active
June 10, 2022 13:42
-
-
Save phunguyen19/fc2e66ed7fadd0b6fcb00242053f4ecf to your computer and use it in GitHub Desktop.
drupal 8, display node field reference entity (label and url)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
/** | |
* 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 %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This also works for any generic nodes, including teaser view modes as long as you are rendering the node.