EntityViewsData::mapFieldDefinition() overwrites existing relations

Created on 16 January 2025, about 2 months ago

Problem/Motivation

Revision based Views of entities are unable to add a relation back to the main entity table.

Core normally adds this in its EntityViewsData::getViewsData() method, here:

      $data[$views_revision_base_table][$entity_id_key]['relationship'] = [
        'id' => 'standard',
        'base' => $views_base_table,
        'base field' => $entity_id_key,
        'title' => $entity_type_label,
        'help' => $this->t('Get the actual @label from a @label revision', ['@label' => $entity_type_label]),
      ];
      $data[$views_revision_base_table][$entity_revision_key]['relationship'] = [
        'id' => 'standard',
        'base' => $views_base_table,
        'base field' => $entity_revision_key,
        'title' => $this->t('@label revision', ['@label' => $entity_type_label]),
        'help' => $this->t('Get the actual @label from a @label revision', ['@label' => $entity_type_label]),
      ];

Then it calls into $this->mapFieldDefinition() for most fields on each of the entity tables.
Entity API's override of that method does not merge the existing Views data for those fields, and instead overwrites it.
This causes the relation added between the base [data] table and the revision [data] table using the entity id fields to be lost.

It appears Core added this relation after Entity API made the override so it was likely overlooked (and Commerce module likely did the same thing for its override.)

The issue was noticed when attempting to recreate the Node revision View bundled with Content Moderation for a custom entity type, and we were unable to add the relation back to the entity's base table.

Steps to reproduce

  1. Create a custom entity type (Foo) which uses Entity API's EntityViewsData class.
  2. Create a new View for the entity's revision table.
  3. Attempt to add a new relation.
  4. The relation "Get the actual Foo from a Foo revision" is not in the list.

Proposed resolution

Perform the same NestedArray::mergeDeep() the base class does in the overridden method.

Remaining tasks

* Implement fix.

User interface changes

None. (Other than restoring the missing relation as a possible option.)

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Active

Version

1.0

Component

Views integration

Created by

πŸ‡ΈπŸ‡ͺSweden twod Sweden

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