6

In Drupal 7 we use following snippet to print taxonomy term field in node.tpl.php

$term = taxonomy_term_load($node->field_example['und'][0]['tid']);

$result = field_view_field('taxonomy_term',$term,'field_desired_field');

echo render($result);

In Drupal 8 how we can print taxonomy term field in node.html.twig?

1
  • 1
    Drupal 8 uses twig instead of templates. To print a variable we use {{ }} and to validate something, we use {% if condition %}. Commented Dec 16, 2016 at 12:29

3 Answers 3

13

To get the field value from a field in a taxonomy term that is referenced in the node:

{{ node.field_example.entity.field_desired_field.value }}

If you want to render a formatted output, use either the ui to configure a custom view mode or use the module Twig Tweak:

{% set tid = node.field_example.target_id %}
{% if tid %}
  {{ drupal_field('field_desired_field', 'taxonomy_term', tid) }}
{% endif %}
6
  • many thanks. this works fine for me and please how can i print image field please. Commented Dec 17, 2016 at 5:02
  • Normally you configure an image field in ui. If you want to do this in twig see the 5th example on the linked page from Twig Tweak.
    – 4uk4
    Commented Dec 17, 2016 at 9:34
  • If my tanonomy field named as: "field_kategorie", it must be the "field_example"? But what is the "field_desired_field"? Commented May 3, 2017 at 6:51
  • @FrankGiesecke, the second field is the field you want to print in the targeted taxonomy term.
    – 4uk4
    Commented May 3, 2017 at 7:04
  • ? That means the same field in my example? Sorry, I don't understand it Commented May 3, 2017 at 7:06
0

The nearest equivalent is described here Rendering arbitrary field values without a parent entity in Drupal 8 and a core issue to make it more reasonable is #2274169 EntityViewBuilder::viewField() / viewFieldItem() don't work for arbitrary added field values

1
  • 1
    This issue does not apply here, because the field has a parent entity.
    – 4uk4
    Commented Dec 16, 2016 at 13:06
-2

In drupal 8, you can do it as -

{% if node.hasField('field_TAXONOMY-TERM') == true %} {{ node.field_TAXONOMY-TERM.value }} {% endif %}

1
  • He means the field that is inside the taxonomy term, not the term name.
    – No Sssweat
    Commented Dec 16, 2016 at 12:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.