Problem/Motivation
The Drupal 7 contrib version of the Views module provided an option for the table style to add CSS classes to the table element itself via the UI. This feature was not implemented when Views was moved to core in Drupal 8. This feature is desirable for site builders and themers because it allows style variations such as table row striping, borderless rows, etc. to be easily added via the UI to specific tables outputted by Views.
Added this functionality will also bring feature parity with the Drupal 7 version in this regard.
Steps to reproduce
- Create a view with a table style.
- Open the table style options modal in the Views UI
- See that there is not a form field to add custom CSS classes to table element.
Proposed resolution
Provide a configuration form option in the Views table style plugin to set custom CSS classes to the table element itself.
Remaining tasks
Convert existing patch to MR.Confirm functionality works with latest core version.Change CSS option to table style configuration schema.Add test to confirm functionality.Add post update hook to update views configuration to match new schema.Add test for post update hook.
User interface changes
A new text input added to the table style options.
Before:

After:

API changes
None
Data model changes
- Add CSS class option to table style plugin configuration schema.
Original report by Anybody
Drupal 8 Follow-up of the Drupal 7 issue #1689472: add class to table in table format, which was committed on 2019-04-04.
Like before in Drupal 7 the Drupal 8 views table formatter only allows to set a class on rows in the UI but not on the table itself, while that would be consistent to other formatters and helpful in several cases.
How can one add a class to the rendered table when a view is outputted as table?
In html-list format for example one can add a class to a wrapper OR/AND to the list. Is this possible to the table as well?
Thanks!
| Comment | File | Size | Author |
|---|---|---|---|
| #58 | 3045871-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #51 | 3045871-after.png | 138.56 KB | pcate |
| #51 | 3045871-before.png | 156.29 KB | pcate |
| #47 | Screen Shot 2024-12-26 at 9.32.44 AM.png | 139.5 KB | pcate |
| #46 | After MR CSS1.png | 107.78 KB | sagarmohite0031 |
Issue fork drupal-3045871
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 #2
anybodyComment #3
anybodyComment #4
pcate commentedI would love to see this option added as well.
Comment #6
dalemoore commentedI was surprised to see this in D7 and not in D8. +1 for porting the D7 solution to D8; I've gotten used to being able to do it in the UI with D7 as of the last year and now slowly moving our sites/creating new sites in D8, this feature is sorely missing.
Comment #10
dieuweI'm hoping to kickstart this with a patch.
I had a look at the D7 implementation and copied that option for the style plugin, and then I updated the theme.inc preprocessing to match what the list plugin already does for its own class option.
Comment #14
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Tagging. for an issue summary update following the standard template please.
This feature request will require test coverage as well
Thanks.
Comment #16
pcate commentedComment #17
pcate commented@smustgrave I've updated the issue summary to follow the standard template.
Comment #19
pcate commentedI've made an initial conversion of @dieuwe #10 patch to a merge request.
Comment #20
pcate commentedComment #21
pcate commentedIt does appear the table style config schema needs to css class option added. The test I added was failing without it. Updated issue summary to reflect this.
Comment #22
pcate commentedComment #23
pcate commentedSetting to "Needs Review". I added a test to confirm functionality is working, and I also tested manually.
Comment #24
anybodyGREAT work @PCate! RTBC! Removing the solved tags!
Comment #25
smustgrave commentedNice!
So this is one of those cases test-only feature doesn't work as it reverts the schema change. So applied locally and reverted the changes but kept the schema, so the test will run
Which shows the test itself.
Believe this one is good. Only thing not 100% sure is if it will need a CR.
Comment #26
dalemoore commentedJust tested—working for me! This just made my day for reals.
Screenshots below.
Comment #27
quietone commentedThe issue summary clearly explains the problem and the change.
This is changing the UI, adding tag. The latest before and after screenshots should be available from the issue summary to help reviewers and committers. I updated the IS to state that they are in #26. Testing was done in #25 and manual testing in #26.
I read the MR and have commented on the user interface text.
Comment #28
anybodyThanks @quietone - you're right! Just did that, think you suggestion is good. I also added "Table" to the title to be even clearer about the context. Please review.
Comment #29
smustgrave commentedBelieve feedback has been addressed here.
Comment #30
longwaveDoes this need an update hook to add the empty class to any existing views that use the table style?
Comment #31
catch#30 is correct, we need a post update for that, otherwise there will be mis-matches when someone next resaves the views UI or a different update runs, marking needs work.
Comment #32
pcate commentedIs there any documentation on how to write config update hooks for views? My understanding is that it is different than regular update hooks?
Comment #33
pcate commentedComment #34
pcate commentedUsing existing views update hooks as examples I tried adding an update hook for the table CSS class schema change. I also added the update hook task to the list of remaining tasks.
Comment #35
smustgrave commentedSo with addition of upgrade path will need a test for the that hook.
Comment #36
pcate commentedComment #37
pcate commentedComment #38
pcate commentedI've added a db schema upgrade test.
Comment #39
pcate commentedComment #40
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #41
pcate commentedComment #42
asawari commentedHi,
Test status -Fail
Steps to reproduce -
Create a view with a table style.
Open the table style options modal in the Views UI
See that there is not a form field to add custom CSS classes to table element.
Issue is reproducible. But unable to apply patch. Can see an error when the patch is applied.
Please see the screenshot.
Comment #43
sagarmohite0031 commentedHi,
Test status -Fail
Issue is reproducible.
MR applied successfully but add custom CSS field is still not there.
Steps to reproduce -
Create a view with a table style.
Open the table style options modal in the Views UI
See that there is not a form field to add custom CSS classes to table element.
Refer Before and after screenshots-
Comment #44
pcate commentedThe test failure was with a unrelated core module. Merging in latest 11.x changes included a fix. Tests are now passing.
After apply the MR to Drupal 11 site install, be sure to run database updates (
drush updb) and clear caches. After applying MR and updates, when you export configuration you should see the schema changes for any views that use a table style.Comment #45
pcate commented@sagarmohite0031 the new CSS class form field should be at the bottom of the table style options form modal. Your screenshot does not show this.
See @dalemoore screenshot in #26 as an example.
Comment #46
sagarmohite0031 commentedHello @pcate,
Attaching Before and after updated screenshots-
Please check once
Comment #47
pcate commented@sagarmohite0031 I apologize, the field input actually is under the row class input. It used to be at the bottom of the modal form, but I forgot it was moved.
I manually tested the MR again with both 10.4 and 11.1 and everything still appears to be working, including see the field input.
Comment #48
smustgrave commentedThink one of the last things needed will be a CR. Also before/after screenshots should be added to the summary for quick glance.
Comment #49
pcate commentedComment #50
pcate commentedI added before/after screenshots to the issue summary and created a draft CR.
Comment #51
pcate commentedComment #52
pcate commentedComment #53
pcate commentedFixed broken images in issue summary. For some reason the did not save previously.
Comment #54
oily commentedRemoved 'Needs change record' tag.
I edited the CR as some information was missing. Please refer to the revision log where I have detailed my changes.
Also I am adding to it that there is a schema change so users may need to run 'drush updb'.
https://www.drupal.org/node/3499943
Comment #55
smustgrave commentedLeft 1 comment on the MR. Think adding a small example would be nice as I don't know if the text I provide if the code is auto generating the selector (.) to the front.
Rest of the feedback appears to be addresses so believe this is 98% there.
Comment #56
pcate commentedI updated the input description text.
Comment #57
smustgrave commentedBelieve all feedback has been addressed
Comment #58
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #59
pcate commentedI merged in the upstream changes from 11.x and fixed the merge conflicts. Setting back to RTBC.
Comment #61
longwaveI read the MR and did a brief manual test and can't see any changes that are required.
Committed ea29660 and pushed to 11.x. Thanks!
Also published the change record.
Comment #63
catchThe post update / ViewsConfigUpdater method added here doesn't handle deprecations.
views_post_update_update_remember_role_emptyandviews_post_update_views_data_argument_plugin_idboth do implement it so could be used for examples.#3443942: Add proper deprecation notices in config entity presave bc layers has more details. The main problems with not adding it are no deprecation notices when tests install modules without outdated exported config, which leads to invalid views in contrib (and core) that may not be compatible with 12.x.
We can open a follow-up to implement that, or revert this and add it back with support, but one of those should happen before 11.2.0 so re-opening for now.
Comment #64
bbralaThink this is a release blocked then.
Comment #65
lendudeOpened #3519443: Add ViewsConfigUpdater deprecation support for update_table_css_class to address this lack of deprecation, also it's not being run from updateAll, so new module config is not getting updated either it looks like
Comment #66
catchThanks! Moving this back to fixed.
Comment #68
acbramley commentedThis deprecation is now triggering fails in another MR that adds a views upgrade path https://git.drupalcode.org/issue/drupal-3095893/-/jobs/5480383