Comment Statistics: Updated/commented date TypeError

Created on 18 April 2025, 27 days ago

Problem/Motivation

A view with the sort criteria Comment Statistics: Updated/commented date (desc) will explode with a

TypeError: Cannot assign null to property Drupal\comment\Plugin\views\sort\StatisticsLastUpdated::$field_alias of type string in Drupal\comment\Plugin\views\sort\StatisticsLastUpdated->query() (line 52 of core/modules/comment/src/Plugin/views/sort/StatisticsLastUpdated.php).

Steps to reproduce

Add a view with the field Comment Statistics: Updated/commented date and add Comment Statistics: Updated/commented date as a sort criteria.

Proposed resolution

/core/modules/comment/src/Plugin/views/sort/StatisticsLastUpdated.php should look more like

<?php

namespace Drupal\comment\Plugin\views\sort;

use Drupal\views\Attribute\ViewsSort;
use Drupal\views\Plugin\views\sort\Date;

/**
 * Sort handler for the newer of last comment / entity updated.
 *
 * @ingroup views_sort_handlers
 */
#[ViewsSort("comment_ces_last_updated")]
class StatisticsLastUpdated extends Date {

  /**
   * The entity table.
   */
  protected ?string $entity_table;

  /**
   * The entity type.
   */
  protected ?string $entity_type;

  /**
   * The entity changed field.
   */
  protected ?string $entity_changed_field;

     /**
   * The field alias.
   */
  protected ?string $field_alias = null;

  /**
   * {@inheritdoc}
   */
  public function query() {
    $this->ensureMyTable();

    // Get the entity type from the base table of the view.
    $this->entity_type = $this->view->storage->get('base_table');

    // Set appropriate entity changed field based on entity type.
    $this->entity_changed_field = $this->getEntityChangedField($this->entity_type);

    // Ensure the entity table is included in the query.
    $this->entity_table = $this->query->ensureTable($this->entity_type, $this->relationship);

    // Add order by using GREATEST to compare entity changed time with last comment time.
    $result = $this->query->addOrderBy(
      NULL,
      "GREATEST(" . $this->entity_table . "." . $this->entity_changed_field . ", " . $this->tableAlias . ".last_comment_timestamp)",
      $this->options['order'],
      $this->tableAlias . '_' . $this->field
    );

    // Make sure we handle the potential null return value
    $this->field_alias = $result ?? $this->tableAlias . '_' . $this->field;
  }

  /**
   * Get the appropriate changed field name for an entity type.
   *
   * @param string $entity_type
   *   The entity type.
   *
   * @return string
   *   The field name that stores the changed timestamp.
   */
  protected function getEntityChangedField($entity_type) {
    // Map common entity types to their changed fields.
    $map = [
      'node' => 'changed',
      'user' => 'changed',
      'taxonomy_term' => 'changed',
      'media' => 'changed',
      // Add more entity types as needed.
    ];

    return $map[$entity_type] ?? 'changed';
  }

}

Remaining tasks

Test and push

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

comment.module

Created by

🇩🇰Denmark Steven Snedker

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

Merge Requests

Comments & Activities

  • Issue created by @Steven Snedker
  • 🇮🇳India annmarysruthy

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies .

  • 🇮🇳India annmarysruthy

    Replicated the issue. The below changes in core/modules/comment/src/Plugin/views/sort/StatisticsLastUpdated.php could fix the issue:

    <?php
    
    namespace Drupal\comment\Plugin\views\sort;
    
    use Drupal\views\Attribute\ViewsSort;
    use Drupal\views\Plugin\views\sort\Date;
    
    /**
     * Sort handler for the newer of last comment / entity updated.
     *
     * @ingroup views_sort_handlers
     */
    #[ViewsSort("comment_ces_last_updated")]
    class StatisticsLastUpdated extends Date {
    
      /**
       * The node table.
       */
      // phpcs:ignore Drupal.NamingConventions.ValidVariableName.LowerCamelName, Drupal.Commenting.VariableComment.Missing
      protected ?string $node_table;
    
      /**
       * The field alias.
       */
      // phpcs:ignore Drupal.NamingConventions.ValidVariableName.LowerCamelName, Drupal.Commenting.VariableComment.Missing
      protected ?string $field_alias;
    
      /**
       * {@inheritdoc}
       */
      public function query() {
        $this->ensureMyTable();
        $this->node_table = $this->query->ensureTable('node_field_data', $this->relationship);
        $this->query->addField($this->node_table, 'changed');
        $this->field_alias = $this->query->addOrderBy(NULL, "GREATEST(" . $this->node_table . ".changed, " . $this->tableAlias . ".last_comment_timestamp)", $this->options['order'], $this->tableAlias . '_' . $this->field);
      }
    
    }

    In the above solution, issue is fixed with fewer changes than the proposed solution in the issue: making $field_alias nullable and using the proper table (node_field_data instead of node).

  • Pipeline finished with Success
    24 days ago
    Total: 1733s
    #478224
  • 🇺🇸United States smustgrave

    Thanks for working on this, can we add some test coverage please

  • Pipeline finished with Failed
    23 days ago
    Total: 165s
    #479094
  • Pipeline finished with Failed
    23 days ago
    Total: 121s
    #479110
  • Pipeline finished with Failed
    23 days ago
    Total: 159s
    #479134
  • Pipeline finished with Failed
    23 days ago
    Total: 144s
    #479144
  • Pipeline finished with Failed
    23 days ago
    #479179
  • Pipeline finished with Running
    23 days ago
    #479181
  • Pipeline finished with Success
    23 days ago
    Total: 425s
    #479191
  • 🇮🇳India annmarysruthy

    Added test. Updated MR. Kindly review

  • 🇺🇸United States smustgrave

    Left some comments, is there not an existing test that could be expanded by chance?

    If you are another contributor eager to jump in, please allow the previous poster(s) at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • Pipeline finished with Failed
    13 days ago
    Total: 245s
    #487053
  • Pipeline finished with Success
    13 days ago
    Total: 1060s
    #487055
  • 🇮🇳India annmarysruthy

    @smustgrave added commits for review comments. Kindly review.

    Checked the existing tests and could not find one related to sorting. Therefore I created a new test.

Production build 0.71.5 2024