Wrong revision can be loaded into original in \Drupal\Core\Entity\EntityStorageBase::doPreSave

Created on 7 March 2023, almost 2 years ago
Updated 21 August 2024, 5 months ago

Problem/Motivation

The \Drupal\Core\Entity\EntityStorageBase::doPreSave loads $this->loadUnchanged($id) into $entity->original when content moderation is being used and if the currently saved entity is not the default revision.

I'm not sure about the impact in core itself, but we see contrib modules working around this with their own additional load mechanisms when using any of the entity hooks where field value comparisons are being made.

Similar issues have been identified and in some cases already being fixed e.g. in πŸ› Impossible to update an entity revision if the field value you are updating matches the default revision. Fixed , πŸ“Œ $entity->original doesn't adequately address intentions when saving a revision Needs work and πŸ“Œ Define 'original' as property on the entity object Needs work .

Proposed resolution

Load the latest revision if available instead of the default revision:

    // Load the original entity, if any.
    if ($id_exists && !isset($entity->original)) {
-      $entity->original = $this->loadUnchanged($id);
+      $entity->original = ($entity instanceof RevisionableInterface && $revision_id = $entity->getLoadedRevisionId()) ?
+        $this->loadRevision($revision_id) :
+        $this->loadUnchanged($id);
    }
πŸ› Bug report
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated 43 minutes ago

Created by

πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024