Steps to reproduce the problem

  1. Create a view using the table display style.
  2. Edit the default field added (Content: Title (Title)).
  3. Open the "Style Settings" dropdown and check "customize field HTML." Choose any HTML element (for this example I'm using P).
  4. Check "create a CSS class" and enter a class (for this example I'm using "p-class").
  5. Apply, save, and view the page.

Table field settings

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>

Table field code output

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.

Issue fork drupal-2238291

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cs_shadow’s picture

I 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.

      if ($classes = $fields[$field]->elementClasses($num)) {
        $column_reference['attributes']['class'][] = $classes;
      }

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.

damiankloip’s picture

Status: Active » Needs review
Issue tags: +VDC, +Needs tests
FileSize
1.33 KB

Here is a fix for that. Spoke to cs_shadow about this on IRC. They are going to do the some test coverage for this.

dawehner’s picture

This has always been the case by design of earl back in the days.

rootwork’s picture

It 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.

rootwork’s picture

FileSize
14.96 KB

I 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.

Table cell with class correctly applied on element

I'm leaving this as needs review as the testbot hasn't processed it yet and it needs tests written anyway.

dawehner’s picture

Status: Needs review » Needs work

I still think we should discuss whether we really have to change this feature, ... this certainly was intended in the first place.

rootwork’s picture

Sure, 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.

Rob230’s picture

Would 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.

rootwork’s picture

Since 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.

Lendude’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.1.x-dev
Component: table style » views.module

Moving this to the right queue.

arlingtonvoicellc’s picture

Looking 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.

rootwork’s picture

@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:

I still think we should discuss whether we really have to change this feature, ... this certainly was intended in the first place.

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.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rootwork’s picture

Bug 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.

rootwork’s picture

Actually 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 either core/themes/stable/templates/views/views-view-table.html.twig or core/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.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Morbus Iff’s picture

Hrms... 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?

ulyngs’s picture

I 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.:

<span class="field_title">[title]</span><br/> 
<span class="field_description">[field_cv_description]</span><br/>
<span class="field_extra">[field_extra_note]</span>

(This was the workaround I used on ulriklyngs.com/cv to give different styling for elements in a table listing curriculum vitae items)

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

m.lebedev’s picture

FileSize
2.8 KB

Quick fix

m.lebedev’s picture

m.lebedev’s picture

FileSize
3.57 KB
m.lebedev’s picture

The 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.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

james.williams’s picture

I 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 that column_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?

rootwork’s picture

Issue tags: +Novice, +Portland2022

I 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

cliddell’s picture

Issue tags: +GiftofOpenSource

[Mentoring] + adding GiftofOpenSource tag

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Mahima_Mathur23’s picture

Rerolled patch from #27 against Version 9.5x as suggested in #34

rootwork’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 37: 2238291-37.patch, failed testing. View results

andregp’s picture

Status: Needs work » Needs review
FileSize
308.62 KB
3.87 KB
1.34 KB

The 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.
test-table before and after
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.

VinmayiSwamy’s picture

VinmayiSwamy’s picture

Please ignore. comment added due to network issue. Apologies.

VinmayiSwamy’s picture

FileSize
474.81 KB

Hello!

I tried to apply patch #40 in Drupal 9.5.x-dev. Patch failed to apply. Attaching the screenshot for error reference.

rootwork’s picture

Status: Needs review » Needs work

rpayanm made their first commit to this issue’s fork.

rpayanm’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

Moving back to NW. Looks like the test cases weren't added.

jsricardo’s picture

Assigned: Unassigned » jsricardo
Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

This isn't ready for review yet

The last patch nor MR contain a test case. And are failing the existing tests.

ameymudras made their first commit to this issue’s fork.

jsricardo’s picture

Assigned: jsricardo » Unassigned

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

marthinal’s picture

FileSize
3.93 KB

Rerolled

marthinal’s picture

FileSize
3.93 KB
467 bytes

We have to use addClass method