TransWikia.com

how to add attributes of the parent taxonomy in the DOM?

Drupal Answers Asked on December 31, 2021

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.

One Answer

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>

Answered by prkos on December 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP