Skip to main content
added 36 characters in body
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

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'] }}

Maybe @4k4's answer below works for you, for me it didn'tonly prints the vocabulary's machine name. So,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 %}

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'] }}

Maybe @4k4's answer below works for you, for me it didn't. So, I 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 %}

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 %}
added 448 characters in body
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

By using the built-in formatters you would achieve that without any templating. You simply can configure the display settings of your content type to have the vocabulary name printed as label or not.

Doing it the way you want, should be considered bad practice, since you are actually invalidating the admin UI. Templates are for changing markup; not for building content.

Nevertheless, here's how you can simply print the field name:

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

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:

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

Maybe @4k4's answer below works for you, for me it didn't. So, I 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 %}

By using the built-in formatters you would achieve that without any templating. You simply can configure the display settings of your content type to have the vocabulary name printed as label or not.

Doing it the way you want, should be considered bad practice, since you are actually invalidating the admin UI. Templates are for changing markup; not for building content.

Nevertheless, here's how you can simply print the field name:

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

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

{{ kint() }}

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'] }}

Maybe @4k4's answer below works for you, for me it didn't. So, I 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 %}
added 6 characters in body
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

By using the built-in formatters you would achieve that without any templating. You simply can configure the display settings of your content type to have the vocabulary name printed as label or not.

Doing it the way you want, should be considered bad practice, since you are actually invalidating the admin UI. Templates are for changing markup; not for building content.

Nevertheless, herehere's how you go.can simply print the field name:

{{ node.field_taxo.entitycontent.bundlefield_taxo_fruit['#title'] }}

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

{{ kint() }}

By using the built-in formatters you would achieve that without any templating. You simply can configure the display settings of your content type to have the vocabulary name printed as label or not.

Doing it the way you want, should be considered bad practice, since you are actually invalidating the admin UI. Templates are for changing markup; not for building content.

Nevertheless, here you go.

{{ node.field_taxo.entity.bundle }}

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

{{ kint() }}

By using the built-in formatters you would achieve that without any templating. You simply can configure the display settings of your content type to have the vocabulary name printed as label or not.

Doing it the way you want, should be considered bad practice, since you are actually invalidating the admin UI. Templates are for changing markup; not for building content.

Nevertheless, here's how you can simply print the field name:

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

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

{{ kint() }}
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126
Loading