1

Let's say I have a content type article with a field "taxonomy".

The vocabulary is "fruits".

The terms are "apple", "orange", "bananas"

The article belongs to "orange".

I can easily print in my twig template the term of the article :

{{ content.field_taxo_fruit }}

Result is "orange".

But I don't find an easy way to print the vocabulary : "fruits".

On an other project, I used views field and I used "rewrite result". But here, I use twig template like node--article--teaser.html.twig

2 Answers 2

2

You can install Devel's submodule Kint, and then in your template print {{ kint() }} to get a pretty-printed dump of all available variables.

Unfortunately I couldn't find the vocabulary label/name in there. Only the field label:

{{ content.field_taxo_fruit['#title'] }}

@4k4's answer below only prints the vocabulary's machine name. But not the label/name. But I finally got hold of the vocabulary name in a preprocess function in MYTHEME.theme from where I sent it over to Twig:

function MYTHEME_preprocess_node__article(&$variables) {

  $node = $variables['node'];

  $vid = array_values($node->field_taxo_fruit->getSettings()['handler_settings']['target_bundles'])[0];

  $vocabulary = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->load($vid);

  $variables['taxo_fruit_vocab'] = $vocabulary->label();
}

And in node--article.html.twig then it's:

{% if taxo_fruit_vocab %}
  <div class="taxo-fruit-vocab">
    Vocab: {{ taxo_fruit_vocab }}
  </div>
{% endif %}
7
  • +1, your approach is what you should try first before digging into the field object like i've tried in my answer.
    – 4uk4
    Commented Jun 14, 2019 at 14:13
  • Perfect, I always try first with admin UI but I hadn't thought of "the label solution". Commented Jun 14, 2019 at 14:23
  • @4k4 I found this interesting : It works but it is not really the taxonomy vocabulary name which is printed but the field name (entity reference field). Am I right ? Commented Jun 14, 2019 at 14:30
  • @SébastienGicquel – Yes indeed. I only noticed it too late. Wait a minute, I'm already looking the vocabulary up. Unfortunately 4k4's solution didn't printed it for me.
    – leymannx
    Commented Jun 14, 2019 at 14:39
  • 2
    No worries, thanks for answering so quickly. @4k4 solution is working partially in my project, it prints the sanitized name of vocabulary (Notice the node.xxxInstead of content.xxx in his answer) I will try your solution and send you a feed back. Commented Jun 14, 2019 at 15:11
3

The vocabulary machine name is the bundle ID of the term, which you can get through the node object:

{{ node.field_taxo.entity.bundle }}

To load the label of the vocabulary:

{{ node.field_taxo.entity.vid.entity.label }}

This works only when the field has content. For an empty field you would have to dig into the field definition you get from the same field object and check whether the field is restricted to a single target bundle.

4
  • Ah yeah! Unfortunately it only prints the vid, not the label.
    – leymannx
    Commented Jun 14, 2019 at 15:02
  • @leymannx, you can load the label in Twig too, because the term references the bundle config entity. I add this to the answer.
    – 4uk4
    Commented Jun 16, 2019 at 13:40
  • You are genius. But from where do you know this? Is it basically just the same pattern for every entity? Or did you dig really deep in what {{ kint() }} returned you? I asked OP to accept your answer instead.
    – leymannx
    Commented Jun 16, 2019 at 13:47
  • @leymannx, this pattern is the same for any content entity with bundles. BTW I think both answers are valid, mine is more literal, yours considering the overall context, although it would need some clarification, machine name (fruits looks like one) or label, vocabulary from the content or field config.
    – 4uk4
    Commented Jun 16, 2019 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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