Steps to reproduce the problem
- Create a view using the table display style.
- Edit the default field added (Content: Title (Title)).
- Open the "Style Settings" dropdown and check "customize field HTML." Choose any HTML element (for this example I'm using P).
- Check "create a CSS class" and enter a class (for this example I'm using "p-class").
- Apply, save, and view the page.
Expected behavior
The HTML for the table cell should look something like:
<td class="views-field views-field-title" headers="view-title-table-column">
<p class="p-class">
<a href="/node/2">title2</a>
</p>
</td>
Actual behavior
<td class="views-field views-field-title p-class" headers="view-title-table-column">
<p>
<a href="/node/2">title2</a>
</p>
</td>
Analysis
The custom class is being placed on the wrapper table cell rather than the HTML element being specified. If you switch to unformatted list style, the class is correctly applied to the P element.
If I am specifying the element I want to wrap the field, the option just below it to specify a class seems like it would apply to that element, not the parent table cell. And it doesn't seem like this behavior should change based on the display style, as it does now.
This behavior is also present in views contrib in Drupal 7.x-3.x. It is related to #1368436: Customize field HTML css class with multiple fields in a column, though that is about what happens to classes on combined fields, whereas this appears to any and every field within a table style.
Comment | File | Size | Author |
---|---|---|---|
#56 | interdiff-55-56.txt | 467 bytes | marthinal |
#56 | 2238291-56.patch | 3.93 KB | marthinal |
#37 | 2238291-37.patch | 3.87 KB | Mahima_Mathur23 |
#27 | 2238291-27.patch | 3.87 KB | m.lebedev |
#26 | 2238291-26.patch | 3.57 KB | m.lebedev |
Issue fork drupal-2238291
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
cs_shadow CreditAttribution: cs_shadow commentedI can confirm that this is an issue just with the table display format. Works well with Grid, HTML List and Unformatted list.
From what I could gather, lines 620-622 in views.theme.inc are causing trouble.
These lines seem to explicitly add the class of that element but are adding class to 'td' instead because they are adding the desired class to
$column_reference['attributes']['class']
instead. IMO we need to add the class to the specified element instead here.I'm a newbie so any suggestions are welcome.
Comment #2
damiankloip CreditAttribution: damiankloip commentedHere is a fix for that. Spoke to cs_shadow about this on IRC. They are going to do the some test coverage for this.
Comment #3
dawehnerThis has always been the case by design of earl back in the days.
Comment #4
rootworkIt seems like an odd choice, in particular because that means it operates differently depending on the style (in list style, the class applies to the element; in table style, the class applies to the parent). And how would one add a class to the element in table style if one wanted to?
Is there documentation about why this choice was made? Perhaps it's time to revisit it now that views is in core.
If we decide to continue operating this way, it should probably be better documented -- I couldn't find any explanation of why this feature was behaving differently under table style than other styles.
Comment #5
rootworkI can confirm that the patch in #2 fixes the issue. Screenshot attached showing the class applied to the element rather than the parent table cell.
I'm leaving this as needs review as the testbot hasn't processed it yet and it needs tests written anyway.
Comment #6
dawehnerI still think we should discuss whether we really have to change this feature, ... this certainly was intended in the first place.
Comment #7
rootworkSure, let's talk about it. I guess I'd just say that if we leave it the way it is, a) it would be nice to have solid justification for having this functionality work so differently (and, to me, unexpectedly), and b) it should be well-documented, because I couldn't find this information anywhere.
I'm definitely curious as to the original reasoning behind it, though.
Comment #8
Rob230 CreditAttribution: Rob230 commentedWould be nice if we can fix it in Drupal 7 as well. I think the current behaviour is contrary to expectation and not what the UI says it does. It has been kept like it to avoid breaking older sites, but it should be possible to add a class to a field inside a table cell, even if it's in addition to the current ability to add it to the header cell.
Comment #9
rootworkSince dawehner said it needs to be talked about more, I wasn't going to push it further. Certainly the general consensus on this issue seems to be that it should be fixed. But perhaps there are other discussions elsewhere about why this is necessary.
I just wish it could be documented somewhere why this strange idiosyncratic behavior is necessary. If it's justified then fine, but right now it's both unexpected and unexplained.
Comment #10
LendudeMoving this to the right queue.
Comment #11
arlingtonvoicellc CreditAttribution: arlingtonvoicellc commentedLooking for a solution to the same problem. In response to #9, this is what I would say:
I have a view setup where I have an "edit" and "view" link grouped into one table column called "actions." Because currently views won't apply the classes to each field, I can't target those individual links for styling. I want the edit link to use a certain background image, and the view link to use a different background. But because the classes are applied to the container, it's not possible to specify which element I'm trying to target.
That's just one basic case. I'm sure others have different uses.
Comment #12
rootwork@arlingtonvoicellc I'm all for the change, I was the original reporter :)
What I was saying in #9 was, given that @dawehner said in #6:
that it'd be nice to know why it was done this way -- i.e. the case for it as it is now -- and, perhaps even more importantly, that that explanation be put into documentation somewhere. Right now this behavior remains unexpected, and unexplained.
Comment #16
rootworkBug is still present. Patch #2 now needs a reroll.
Still not sure why this was the case in the first place, or why it shouldn't be changed.
The example in #11 is exactly the situation that I ran into that confirmed that this still happens.
Comment #17
rootworkActually it looks like this needs more than a reroll; because theme functions are now in Twig templates, this needs to be accomplished in a different way.
The relevant templates are
core/modules/views/templates/views-view-table.html.twig
and eithercore/themes/stable/templates/views/views-view-table.html.twig
orcore/themes/classy/templates/views/views-view-table.html.twig
depending on your theme.I attempted to move the
column_classes
from the<td>
to the field itself, but I think that would take some more refactoring (possibly a field template specifically for the table style) since they're actually in different templates. In any case, I wasn't up to the task. If anyone else is looking to do this, I'd start there.Comment #19
Morbus IffHrms... Ok, if this goes in, what would be the proper way, through the Views UI, to add a class to the table cell? I've been using the approach described within because I /want/ the class on the cell. If this patch gets in, what's the alternative?
Comment #20
ulyngs CreditAttribution: ulyngs commentedI just ran into the same issue - it makes it very cumbersome to do styling of different information coming from the different fields.
The work-around solution I discovered was to let just one field be shown in the table, but rewrite the output of that field with HTML using replacement patterns referring to the other fields and which assigns the CSS classes I want, e.g.:
(This was the workaround I used on ulriklyngs.com/cv to give different styling for elements in a table listing curriculum vitae items)
Comment #24
m.lebedev CreditAttribution: m.lebedev commentedQuick fix
Comment #25
m.lebedev CreditAttribution: m.lebedev commentedComment #26
m.lebedev CreditAttribution: m.lebedev commentedComment #27
m.lebedev CreditAttribution: m.lebedev commentedThe patch allows to set preferences for the html wrapper and the html element. The label wrapper class setting does not work, because label classes add to "th" by default.
Comment #33
james.williams CreditAttribution: james.williams at ComputerMinds commentedI just ran into this with a colleague. The 'solution' from comment 20 sounds like the way to go for site builders, whilst the bug exists.
For us, we found that the classes from the previous column were re-used again for a column, because
column.default_classes
was set to false (no idea why though, as the 'Add default classes' setting was definitely ticked for the field), which meant thatcolumn_classes
in the template got carried over from the previous iteration of the loop, rather than being re-initialised/cleared (see the template code here). So not quite the same problem, but the same solution was necessary.Even if a coded solution was made (whether to adjust where the classes go, or just to avoid that daft re-use of classes from a previous column), I guess it would by definition make for markup changes which might not be wanted for backwards-compatibility?
Comment #34
rootworkI think this could potentially be a good novice task for someone who knows or wants to learn Twig -- at least to reroll the patch from #27
Comment #35
cliddell CreditAttribution: cliddell at CivicActions commented[Mentoring] + adding GiftofOpenSource tag
Comment #37
Mahima_Mathur23 CreditAttribution: Mahima_Mathur23 at Srijan | A Material+ Company for Drupal India Association commentedRerolled patch from #27 against Version 9.5x as suggested in #34
Comment #38
rootworkComment #40
andregp CreditAttribution: andregp at CI&T commentedThe fail at #27 (or #37 as they have the same code) was related to an extra trailing space being added after the elements.
Here you can see the comparison between the outcomes of the second call
$this->drupalGet('test-table');
(line 112) inside StyleTableTest::testFieldInColumns() before and after the patch.You can see that there are extra trailing spaces after the patch was applied, which triggered the error on the test. I hope this new patch solves the issue.
Comment #41
VinmayiSwamy CreditAttribution: VinmayiSwamy as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #42
VinmayiSwamy CreditAttribution: VinmayiSwamy as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedPlease ignore. comment added due to network issue. Apologies.
Comment #43
VinmayiSwamy CreditAttribution: VinmayiSwamy as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedHello!
I tried to apply patch #40 in Drupal 9.5.x-dev. Patch failed to apply. Attaching the screenshot for error reference.
Comment #44
rootworkComment #47
rpayanmComment #48
smustgrave CreditAttribution: smustgrave at Mobomo commentedMoving back to NW. Looks like the test cases weren't added.
Comment #49
jsricardo CreditAttribution: jsricardo at CI&T commentedComment #50
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis isn't ready for review yet
The last patch nor MR contain a test case. And are failing the existing tests.
Comment #52
jsricardo CreditAttribution: jsricardo at CI&T commentedComment #55
marthinal CreditAttribution: marthinal at Bluespark commentedRerolled
Comment #56
marthinal CreditAttribution: marthinal at Bluespark commentedWe have to use addClass method