Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Get Taxonomy Term Custom Field Value in node.html.twig - Drupal 8

When working with taxonomies in Drupal 8, you may find yourself needing to add custom fields to taxonomy terms for specific vocabularies. In fact, it's often the case that each taxonomy vocabulary will have its own set of custom fields assigned to it, for site management purposes.

Of course, it's possible to then reference these terms for individual nodes, creating a powerful tagging system.

However, what if you find yourself needing to access the value of a custom field you've created for your taxonomies, specifically when rendering the node contents in your node.html.twig file?

The solution is simple!

Here's the syntax you need to use in your node.html.twig file to get the field value for a taxonomy term:

{{ node.field_taxonomy.entity.field_color.value }}

In this example, there are two field names in the syntax you need to change:

"field_taxonomy" refers to the machine name of the taxonomy term reference field on your node, and "field_color" refers to the machine name of the custom taxonomy field for which you need to access.

Adding this simple syntax within your node.html.twig file will save you time and a lot of hassle. Now you can easily render field values for taxonomies referenced in your node.

Don't forget to clear the Drupal caches after saving your file!