Search code examples
drupaltwigtaxonomytemplatingmedia-library

How do I show the Drupal Media library file media tag in a paragraph template?


When you have the Media library installed in Drupal you have the ability to tag your media in order to sort the images and other media. This tag field is a reference to a Taxonomy term. I was wondering if it is possible to show this taxonomy term in a Paragraph template.


Solution

  • You can do this using the Twig tweak module!

    First you get the media targ target id:

    {% set tid = content.field_img["#items"].entity.get("field_media_tags").0.target_id %}
    

    Next you can get the taxonomy term path like this (first you check if there is an actual numeric taxonomy id present, otherwise return an empty string):

    {% set taxonomy_term_path = (tid matches '/^\\d+$/' ? (path('entity.taxonomy_term.canonical', {'taxonomy_term': tid})) : "") %}