Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Getting a Taxonomy Term URL Path From a Reference Field - Twig - Drupal 8

So you've got a taxonomy reference field on your content type, block type, View, or somewhere else.

The taxonomy term reference field basically allows you to associate a piece of content with a node.

But what if you need to get the URL or path of the taxonomy term that you've referenced in your piece of content?

It's actually fairly easy.

In Twig, all you need to do is this:

{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_taxonomy_reference.entity.tid.value}) }}

Or, you might need to use a variation of the syntax, replacing 'node' with content', like this:

{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': content.field_taxonomy_reference.entity.tid.value}) }}

Leave the entire syntax as exampled above. The only thing you need to change here is the field name and entity type, which are highlighted in bold above.

Here's an example of how you could potentially use this code in your template file:

<a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_taxonomy_reference.entity.tid.value}) }}">
  <h2>{{ content.field_taxonomy_reference }}</h2>
</a>