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!

Related Questions

How to create and check block revisions?

1  Asked on November 8, 2021 by user3560198


Custom module with config exposed in json api

1  Asked on November 8, 2021 by daniel-harper


Not able to see < character in front end

2  Asked on November 5, 2021 by jos-l


Limit filter operators

1  Asked on November 5, 2021


Dynamic filters based on another filter

1  Asked on November 5, 2021 by soundcheese


How to add a field to menus?

1  Asked on October 26, 2021


Drush cr is responding with Bootstrap Failed

0  Asked on October 26, 2021 by chanel


How do I create a table when a module is installed?

1  Asked on October 26, 2021 by david-montera


Approaches for multi-field values

0  Asked on October 26, 2021 by ian-stewart


Pager not being rebuilt after $form_state->setRebuild() is called

0  Asked on October 26, 2021 by kenny83


Where are Commerce 2 hooks (or Events) in Commerce 2

1  Asked on October 26, 2021 by wally-kolcz


Show own content in Media Library

0  Asked on October 26, 2021 by rafaticarte


Ask a Question

Get help from others!

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