Views table has multiple active sort columns when "Add default classes" is disabled

Created on 26 January 2024, 10 months ago
Updated 12 February 2024, 9 months ago

Problem/Motivation

If a column in a view has the option "Add default classes" turned off, then its header and data cells get the same classes as the prior column. This can lead to more than one column seeming to be "active" for sorting, and other possible problems with incorrect classes being applied.

Steps to reproduce

Install the attached view. In the views UI, note that the first field (Title) has "Add default classes" turned on, whereas the second field (Authored On) has it turned off. This results in the Authored On field getting the is-active class when it should not.

Proposed resolution

The bug occurs twice in the views-view-table.html.twig that is part of every theme. The first time is:

      <tr>
        {% for key, column in header %}
          {% if column.default_classes %}
            {%
              set column_classes = [
                'views-field',
                'views-field-' ~ fields[key],
              ]
            %}
          {% endif %}

Because column_classes is not reset when column.default_classes (the "Add default classes" option) is turned off, the list of classes is reused from the previous column. The solution is simply to add a line:

      <tr>
        {% for key, column in header %}
          {% set column_classes = [] %} {# ADDED THIS LINE #}
          {% if column.default_classes %}

A similar fix must be made further down in the template, where the data cells are generated. The attached patch addresses this problem for all of the themes in core, as well as the default template in Views itself.

It's worth noting that any custom themes that include a version of this template, such as the contrib version of Classy โ†’ , also need this fix.

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Viewsย  โ†’

Last updated about 13 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States gribnif

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024