When you have an entity ID value such as node ID (nid) or taxonomy term ID, how can you create the alias in a twig file? In other words, instead of /taxonomy/term/{tid} I would like to get the alias I have defined for this, eg: /some/path/to/my/term.
In trying to find an answer you might have queried your search criteria using something like
twig canonical taxonomy path twig canonical entity path twig canonical node path
How to achieve
This is achieved through using the following
{% set alias = path('entity.{type}.canonical', {'{ref}': tid}) %}
Where type can be: node or taxonomy_term to label a couple. And ref is node or taxonomy_term respectively. So how does this look against the two entities?
Node alias path:
{% set alias = path('entity.node.canonical', {'node': tid}) %}
Taxonomy term path:
{% set alias = path('entity.taxonomy_term.canonical', {'taxonomy_term': tid}) %}
Seeing this in action. In the scenario below, presenting a node, that has a taxonomy reference field category
{% for category in node.field_category %} {% set tid = category.entity.tid.value %} {% set term = category.entity.name.value %} {% set alias = path('entity.taxonomy_term.canonical', {'taxonomy_term': tid}) %} <a href="{{ alias }}" hreflang="en">{{ term }}</a> {{ loop.last ? "" : ", " }} {% endfor %}