While theming a views-view-list.html.twig file I noticed adding a class to the list.attributes didn't work on line 28 of views-view-list.html.twig:
<{{ list.type }}{{ list.attributes.addClass('some-class') }}>
The issue seems to be that since I don't have any classes added to the List class field in the Format > Settings > Style options for my HTML List the list.attributes
is never instantiated as an Attribute
.
Comment | File | Size | Author |
---|---|---|---|
#24 | 2845400-24.patch | 1009 bytes | Gauravvvv |
| |||
#5 | views-fix-list-attributes-in-twig-file-2845400-5.patch | 947 bytes | Munavijayalakshmi |
#2 | views-fix-list-attributes-in-twig-file-2845400-2.patch | 927 bytes | Daniel Korte |
Comments
Comment #2
Daniel KorteThe attached patch instantiates the
list.attributes
whether the List class field is empty or not.Comment #3
LendudeYeah I've run into this in other parts of core too, annoying to have to do a hook_preprocess to just add the Attribute. I did a quick scan of the rest of
views.theme.inc
and this seems the only instance where this seems relevant.Never looked into the testing side of theme stuff, can we/should we test this?
Comment #5
Munavijayalakshmi CreditAttribution: Munavijayalakshmi at Valuebound commentedRerolled the patch.
Comment #6
Daniel KorteThanks for the reroll! It applies cleanly to 8.3.2
Comment #7
mlncn CreditAttribution: mlncn at Agaric for Drutopia commentedIt also works great! RTBC
Comment #8
alexpottNo one's answered @Lendude's question about whether this should be tested. It would be good at least to know if we have exiting test coverage of adding a class before making this change.
Comment #9
Manuel Garcia CreditAttribution: Manuel Garcia as a volunteer commentedHi, just randomly found this issue while looking at views module issues, and thought I'd chip in.
If this is happening on other parts of core, I wonder whether we should do the test for all of core templates, which would help us identify every instance where this is happening. I'm guessing we should perhaps be patching
template_preprocess()
? Should we open a follow up?Comment #12
jwilson3I'd like to take this one step further and only instantiate the Attribute if it hasn't already been created somewhere else.
It's pretty complicated for mere mortals to guarantee the order in which preprocess functions run, so if someone has setup Attributes in another preprocess for a contrib/custom module or theme, they might be blown away by this.
Wrapping the statement in an if or ternary operator could fix this:
Would be nice to standardize on this as the preprocess way throughout core/contrib.
In the same vein, if you're just trying to add classes in the twig template, you can make use of the
create_attribute()
twig command, shown here in the context of thelist.attributes
in a customviews-view-list.html.twig
using ternary operator:Or the long hand form of the above:
Comment #14
Daniel KorteThis same issue happens in pager.html.twig too. Adding a class to the item attributes does not work when no other attributes have been added:
{{ item.attributes.addClass('foo') }}
Comment #23
MegaKeegMan CreditAttribution: MegaKeegMan at Agaric for Drutopia, Portside commentedCan confirm that this patch does not apply, but is absolutely still needed.
Comment #24
Gauravvvv CreditAttribution: Gauravvvv at Axelerant for Drupal India Association commentedI have attached a patch for same, please review
Comment #25
smustgrave CreditAttribution: smustgrave at Mobomo commentedNext step would be to get a test case to show the issue.
Comment #26
MegaKeegMan CreditAttribution: MegaKeegMan at Agaric for Drutopia, Portside commentedAt the very least I can say that patch #24 does apply and does fix the issue. Tested on Drupal 9.5.4