Add Twig filter to add classes

Created on 15 April 2023, over 1 year ago
Updated 2 May 2023, over 1 year ago

Problem/Motivation

The method of adding a class attribute looks like this:

{{ content.field_image|add_attr('image_attributes', {class: ['custom-class']}) }}

This works fine but, given that adding a class is the most likely way this module is used, it would be helpful to have a filter that handles only classes, which would simplify the code a little. It would also echo Drupal's setAttribute() and addClass() attribute methods.

Proposed resolution

Create a new filter that accepts a string (or an array of strings) and adds it to the specified attributes element.

Remaining tasks

  1. Test
  2. Commit

User interface changes

None.

API changes

A new Twig filter can be used to add classes to an attribute element.

Data model changes

None.

✨ Feature request
Status

Closed: outdated

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States rlhawk Seattle, Washington, United States

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024