Views ‘last updated/commented’ field fails to sort entities without comment statistics

Created on 12 August 2025, 3 days ago

Problem/Motivation

In Drupal 10, the Views field/sort for
“Comment Statistics: Updated/commented date”
fails to handle entities without a corresponding
comment_entity_statistics (CES) row.

In Drupal 7, CES rows were populated broadly, so
GREATEST(node.changed, last_comment_timestamp) always produced a value.
In Drupal 10, CES is only populated for certain node types with comments enabled, so
GREATEST(..., comment_entity_statistics.last_comment_timestamp) can evaluate to NULL.
This breaks default/table click-sorting and filtering on that column: items without comments
sort incorrectly (or appear missing when sorted by that field) even if recently updated.

Steps to reproduce

  1. Create two node types: One supporting comments the other without.
  2. Create two nodes of each type.
  3. Create a View listing those node types in a table.
  4. Add the field “Comment Statistics: Updated/commented date”
  5. Enable sorting on that field (default sort or click-sort in a table display).
  6. Observe: nodes without comments sort to the top as their value is NULL from the CES table.

Proposed resolution

  • Update the Views handler SQL to use a NULL-safe fallback before GREATEST(), e.g.:
    GREATEST(
      node_field_data.changed,
      COALESCE(comment_entity_statistics.last_comment_timestamp, node_field_data.changed)
    )
  • Add test coverage for entities without CES rows to ensure sorting/filtering behaves correctly.

Release notes snippet

Fixed sorting/filtering for the Views field
“The most recent of last comment posted or entity updated time” when an entity has no
comment_entity_statistics row. The field now falls back to the entity’s
changed timestamp to ensure consistent behaviour across content with and without comments.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

comment.module

Created by

🇨🇦Canada joelpittet Vancouver

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

Merge Requests

Comments & Activities

  • Issue created by @joelpittet
  • 🇨🇦Canada joelpittet Vancouver

    I will postpone this on 🐛 Last read comment field/filter/argument uses still the node.changed instead of node_field_data.changed column Needs work because it will likely conflict.

    But a workaround in the mean time if anybody else needs this for table click sorting.

    modules/custom/CUSTOM_MODULE/CUSTOM_MODULE.views.inc

    /**
     * Implements hook_views_data_alter().
     */
    function CUSTOM_MODULE_views_data_alter(array &$data):void {
      // Replace the core comment CES "last updated" sort handler with our NULL-safe version.
      if (isset($data['comment_entity_statistics']['last_updated']['sort'])) {
        $data['comment_entity_statistics']['last_updated']['field']['id'] = 'comment_ces_last_updated_nullsafe';
        $data['comment_entity_statistics']['last_updated']['sort']['id'] = 'comment_ces_last_updated_nullsafe';
      }
    }
    
    

    modules/custom/CUSTOM_MODULE/src/Plugin/views/field/NullSafeStatisticsLastUpdated.php

    
    namespace Drupal\CUSTOM_MODULE\Plugin\views\field;
    
    use Drupal\views\Attribute\ViewsField;
    use Drupal\views\Plugin\views\field\Date;
    
    /**
     * NULL‑safe field for newer of last comment / entity updated.
     */
    #[ViewsField('comment_ces_last_updated_nullsafe')]
    class NullSafeStatisticsLastUpdated extends Date {
    
      public function query() {
        $this->ensureMyTable();
        $entity_type = \Drupal::entityTypeManager()->getDefinition($this->getEntityType());
        $entity_data_table = $this->query->ensureTable($entity_type->getDataTable(), $this->relationship);
    
        $this->field_alias = $this->query->addField(
          NULL,
          "GREATEST({$entity_data_table}.changed, COALESCE({$this->tableAlias}.last_comment_timestamp, {$entity_data_table}.changed))",
          $this->tableAlias . '_' . $this->field
        );
    
      }
    }
    

    modules/custom/CUSTOM_MODULE/src/Plugin/views/sort/NullSafeStatisticsLastUpdated.php

    
    namespace Drupal\CUSTOM_MODULE\Plugin\views\sort;
    
    use Drupal\views\Attribute\ViewsSort;
    use Drupal\views\Plugin\views\sort\Date;
    
    /**
     * NULL‑safe sort for newer of last comment / entity updated.
     */
    #[ViewsSort('comment_ces_last_updated_nullsafe')]
    class NullSafeStatisticsLastUpdated extends Date {
    
      public function query() {
        $this->ensureMyTable();
        $entity_type = \Drupal::entityTypeManager()->getDefinition($this->getEntityType());
        $entity_data_table = $this->query->ensureTable($entity_type->getDataTable(), $this->relationship);
    
        $this->field_alias = $this->query->addOrderBy(
          NULL,
          "GREATEST({$entity_data_table}.changed, COALESCE({$this->tableAlias}.last_comment_timestamp, {$entity_data_table}.changed))",
          $this->options['order'],
          $this->tableAlias . '_' . $this->field
        );
      }
    }
    
    
  • 🇨🇦Canada joelpittet Vancouver
  • 🇨🇦Canada joelpittet Vancouver

    Add regression tests for comment statistics sort with and without comment supported node types, so we don't need to do that later.

  • Pipeline finished with Failed
    3 days ago
    Total: 2188s
    #571326
  • 🇨🇦Canada joelpittet Vancouver

    This test failure seems unrelated — it’s complaining about a missing schema definition for click_sort_column, but that property is already used in many places in core. A bit annoying to have to fix this, and I’m not sure whether it should be addressed here or spun off into a separate follow-up, though a quick issue search talks about it here 🌱 Make views consistent in code, schema, tests, and APIs Needs review .

    There should be no errors in configuration 'views.view.test_comment_last_updated_click_sort'. Errors:
    ├ Schema key views.view.test_comment_last_updated_click_sort:display.default.display_options.fields.comment_entity_statistics_last_updated.click_sort_column failed with: missing schema

Production build 0.71.5 2024