Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In Twig templates, calling <div{{ attributes.addClass() }}>
results in <div class="">
. When no attributes exists and addClass()
is called without any arguments, it adds an empty class definition.
I discovered this while working on #2329759-9: Move container classes from preprocess to templates
Proposed resolution
Remaining tasks
API changes
none
Comment | File | Size | Author |
---|---|---|---|
#1 | drupal-attribute-addclass-2330731-1.test-must-fail.patch | 699 bytes | Sutharsan |
#1 | drupal-attribute-addclass-2330731-1.patch | 3.18 KB | Sutharsan |
Comments
Comment #1
Sutharsan CreditAttribution: Sutharsan commentedThis patch adds an if-condition to
addClass()
to prevent adding a single empty class.Comment #3
Sutharsan CreditAttribution: Sutharsan commentedLast test failed on purpose. Back to needs review.
Comment #4
star-szrLooks good, makes sense, and adds test coverage. RTBC, thanks @Sutharsan!
Comment #5
star-szrComment #6
alexpottCommitted 4697eb9 and pushed to 8.0.x. Thanks!
Comment #8
star-szrFollow-up posted because unfortunately this didn't really fix the usage case in #2329759: Move container classes from preprocess to templates.
#2336355: Refactor Attribute rendering so that class="" is not printed