Change record status: 
Project: 
Introduced in branch: 
10.1.x
Introduced in version: 
10.1.0
Description: 

The new |add_class and |set_attribute Twig filters allow for setting CSS classes or attributes on field render arrays. This mitigates the need for modifying field templates or preprocessing just to add a CSS class.

Examples

  • {{ content.field_tags|add_class('my-class') }} adds the my-class CSS class to the field template for field_tags.
  • {{ content.field_tags|set_attribute('aria-hidden', 'true') }} adds the aria-hidden="true" attribute and value to the field template for field_tags.
Impacts: 
Themers

Comments

lussoluca’s picture

Maybe is not "|add_class and |add_suggestion" but "|add_class and |set_attribute" ?

mherchel’s picture

kevinhurley23’s picture

Am I misunderstanding what this is supposed to do? I am trying to use this filter on a link field like this:
{{ content.field_card_link|add_class('btn') }}
And the resulting link element does not have any classes. This is on Drupal 10.2.0.

alanburke’s picture

The wrapper will have the class, not the a tag itself.