Content Moderation moderation_state_filter cannot join the entity revision table when the filter uses relationship to the entity revision table

Created on 25 October 2023, about 1 year ago
Updated 11 March 2024, 10 months ago

Problem/Motivation

The "moderation_state_filter" views filter defined in "core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php" does not correctly join the entity's base table when the views filter is using a views relationship that relates the entity's revision data table.
Relate code in "ModerationStateFilter::opSimple()":

        $entity_base_table_alias = $this->relationship ?: $this->table;

        // The bundle field of an entity type is not revisionable so we need to
        // join the base table.
        $entity_base_table = $entity_type->getBaseTable();
        $entity_revision_base_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
        if ($this->table === $entity_revision_base_table) {
          $configuration = [
            'table' => $entity_base_table,
            'field' => $entity_type->getKey('id'),
            'left_table' => $entity_revision_base_table,
            'left_field' => $entity_type->getKey('id'),
            'type' => 'INNER',
          ];

          $join = Views::pluginManager('join')->createInstance('standard', $configuration);
          $entity_base_table_alias = $this->query->addRelationship($entity_base_table, $join, $entity_revision_base_table);
        }

BUG: Using the 'left_table' as $entity_revision_base_table only works if the view's base table is the entity's revision base table, example "node_field_revision".
This bug results in the following error and WSOD:

Warning: Trying to access array offset on value of type null in Drupal\views\Plugin\views\join\JoinPluginBase->buildJoin() (line 311 of {WEBROOT]/core/modules/views/src/Plugin/views/join/JoinPluginBase.php) 

The 'left_table' should be $entity_base_table_alias which is the $this->relationship or the $this->table, which at this point $this->table is $entity_revision_base_table.

Currently, there is no test coverage for this bug. The current "Drupal\Tests\content_moderation\Kernel\ViewsModerationStateFilterTest" uses a "node_field_data" relationship so it will not test the code within if ($this->table === $entity_revision_base_table) {.

Steps to reproduce

  1. Using a hook_views_data_alter, add a view relationship for the views data of a table that can join to the entity's revision data table. Example: A new relationship from "user_field_data.uid" to "node_field_revision.uid").
  2. Create a view for the User
  3. Add the new relationship to relate the "node_field_revision".
  4. Add an exposed views filter for "Moderation State" that uses the relationship for the "node_field_revision".
  5. Add a page display with a path
  6. Save the view.
  7. View the page for this view.
  8. Select "Draft" for the Moderation State filter
  9. Submit the views form
  10. RESULT: BUG - WSOD with a log message of "Warning: Trying to access array offset on value of type null in Drupal\views\Plugin\views\join\JoinPluginBase->buildJoin() (line 311 ..."

Proposed resolution

The 'left_table' should be $entity_base_table_alias which is the $this->relationship or the $this->table, which at this point $this->table is $entity_revision_base_table.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

πŸ› Bug report
Status

Fixed

Version

10.2 ✨

Component
Content moderationΒ  β†’

Last updated 24 days ago

  • Maintained by
  • πŸ‡¦πŸ‡ΊAustralia @Sam152
Created by

πŸ‡ΊπŸ‡ΈUnited States recrit

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

Comments & Activities

Production build 0.71.5 2024