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
    Drupal 8 uses twig instead of templates. To print a variable we use {{ }} and to validate something, we use {% if condition %}. – Ashish Deynap Dec 16 '16 at 12:29
8

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 %}
  • many thanks. this works fine for me and please how can i print image field please. – Farhan Ijaz Dec 17 '16 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. – 4k4 Dec 17 '16 at 9:34
  • If my tanonomy field named as: "field_kategorie", it must be the "field_example"? But what is the "field_desired_field"? – Frank Giesecke May 3 '17 at 6:51
  • @FrankGiesecke, the second field is the field you want to print in the targeted taxonomy term. – 4k4 May 3 '17 at 7:04
  • ? That means the same field in my example? Sorry, I don't understand it – Frank Giesecke May 3 '17 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

  • This issue does not apply here, because the field has a parent entity. – 4k4 Dec 16 '16 at 13:06
0

In drupal 8, you can do it as -

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

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

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