Needs work
Project:
Drupal core
Version:
main
Component:
views.module
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
20 Jan 2017 at 17:42 UTC
Updated:
7 Jan 2026 at 07:03 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
daniel korteThe attached patch instantiates the
list.attributeswhether 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.incand 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 commentedRerolled the patch.
Comment #6
daniel korteThanks for the reroll! It applies cleanly to 8.3.2
Comment #7
mlncn 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 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.attributesin a customviews-view-list.html.twigusing 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 commentedCan confirm that this patch does not apply, but is absolutely still needed.
Comment #24
gauravvvv commentedI have attached a patch for same, please review
Comment #25
smustgrave commentedNext step would be to get a test case to show the issue.
Comment #26
megakeegman commentedAt the very least I can say that patch #24 does apply and does fix the issue. Tested on Drupal 9.5.4
Comment #29
mlncn commentedMerge request !13162 is a straightforward reroll— exactly what is in daniel korte's patch initial patch in #2 got into core through changes made out of this issue (without tests?!), so technically the problem stated in the title of this issue is now fixed in Drupal core. My re-roll of a merge request therefore only preserves jwilson's proposal in #12, implemented in gauravvvv's patch in #24, to not blow away attributes that were added by a possibly earlier preprocess.
I agree this is how it should work, but am not up for a test, let alone making this consistent everywhere, and i know there are rumblings about somehow getting rid of
hook_preprocessentirely, however that would work, so i leave this humble re-roll and update as my offering.Comment #30
smustgrave commentedThanks for re-rolling!
Tagging for issue summary as the standard template appears to be missing.
Based on the title seems like something that tests should cover so am leaving that tag.
Thanks!
Comment #31
mlncn commented