I am displaying a taxonomy "category" on a node, a view of that node ,and a search facet.

This taxonomy has 2 levels.

How can i add to the rendered HTMLs, i guess using preprocessing hooks, an attribute indicating the taxonomy parent id ?

I would need to apply CSS differently to the taxonomy buttons depending on the parent.

1 Answer 1


You can add the relevant Taxonomy parent Field to your Views, make it display the term ID. This way you get that information available as a token.

You can hide that field with Exclude from display and position it above all the other fields, so you can use it as a token in all of them without worrying about the position.

Then edit the Style settings of your "Parent" field that you want to style and enable the Create a CSS class option. You can enter the token for your parent TID in there.

Since css classes can't start with numbers make sure to enter a safe start for the class, for example: myclass-{{ tid }}

To find the list of tokens you'll have to temporarily enable the option Rewrite results > Override the output of this field with custom text > Replacement patterns.

You may need to Customize field and label wrapper HTML and/or Use field template depending on your particular configuration and output to get the class to show up.

Alternatively, you can construct the output of your field on your own with the Override the output of this field with custom text and enter what you want the HTML to show:

<div class="myclass-{{ tid }}">{{ field_output }}</div>

  • the tid does not give info about the hierarchy though
    – Matoeil
    May 30, 2022 at 12:49
  • Tid doesn't show hierarchy directly, you can combine it with the information you get from Parent field, or use the Parent field to nest results.
    – prkos
    May 30, 2022 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.