column_classes not consistently reset between fields in views-view-table.html.twig

Created on 26 March 2023, about 2 years ago
Updated 24 January 2024, about 1 year ago

Problem/Motivation

If you have a view using the views-view-table.html.twig template, with some fields/columns set to display default classes and others not ("Add default classes" checked or not under Style Settings), any field/column that is not meant to get default classes will instead get the default classes of the last field/column to be rendered before it that does have "Add default classes" checked. This is because the column_classes Twig variable is not reset on every iteration through the column loop, but only for those columns where "Add default classes" was checked.

<!--break-->

Steps to reproduce

  1. Create a view using the Table display format and multiple fields, with the default views-view-table.html.twig template
  2. Set at least the first field to "Add default classes" (check field under "Style Settings"
  3. Set at least one latter field to not "Add default classes" (un-check field under "Style Settings")
  4. Inspect the <td> classes of the rendered table

Any of the no-default-classes fields ("columns" in the template) that follow a field that is set to display default classes will be found to have the same classes as that previous field. If multiple no-default-classes columns appear in sequence, they all will get the same classes as the last field with default classes rendered before them.

Proposed resolution

The column_classes variable should be reset on each loop through the row columns. As it is now, the variable is only "reset" on those iterations where default_classes is set.

There are two ways this could be done. I'll leave it to the experts to determine which ways is better:

  1. Reset the variable at the top of the column loop (immediately after {% for key, column in row.columns %}) with {% set column_classes = [] %}. This is the simplest option at only one new line of code, but it is slightly redundant for columns where default_classes is set (likely the majority of cases).
  2. Add an {% else %} condition to the default_classes condition block ({% if column.default_classes %}) and reset the variable there ({% set column_classes = [] %}). This adds two lines of code and creates a more complex logical structure for the themer to navigate, but it has the benefit of more explicitly demonstrating how classes are being handled by the default_classes setting.

Remaining tasks

  • Decide which solution to implement
  • Consider implications of change & communication needs: developers/themers may have sites that inadvertently depend on the current behavior. (With this change, classes will disappear from <td> elements where they had previously appeared.)
πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
OtherΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States daletrexel Minnesota, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @daletrexel
  • Assigned to nicxvan
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    This is a core issue actually, not views_templates.

    To further your explanation, the column classes are only reset if the default value is set.

    I'll move it over and take a look later.

Production build 0.71.5 2024