On a node template , i am displaying a taxonomy term ( entity ref field ). That taxonomy term has a parent. How can have that info on the html generated , for css purposes ?

The field.html.twig display this:

<div class="article__categorie-article">
              <div><a href="/taxonomy/term/27" hreflang="fr">Utilisation de Drupal</a></div>
              <div><a href="/taxonomy/term/29" hreflang="fr">Rôle</a></div>

I would like to have smth like

<div class="article__categorie-article">
              <div><a href="/taxonomy/term/27" hreflang="fr" data-parent-tid="2">Utilisation de Drupal</a></div>
              <div><a href="/taxonomy/term/29" hreflang="fr" data-parent-tid="4">Rôle</a></div>
  • 1
    My suggestion for this would be to create an array to pass to the twig template. In .theme file, use an alter function. Create a $variables['taxonomy'] = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('articles'); and then now in the twig file you can use the taxonomy variable. – Armando Silva Sep 26 '20 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.