Commit bf994712 authored by catch's avatar catch
Browse files

fix: #3594092 loadUnchanged() returns an in-memory-modified entity when...

fix: #3594092 loadUnchanged() returns an in-memory-modified entity when hook_entity_preload() swaps in a non-default revision

By: amateescu
By: godotislate
(cherry picked from commit aad45020)
parent aec98644
Loading
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -1396,6 +1396,16 @@ public function loadUnchanged($id) {
    // by explicitly removing the entity from the static cache.
    parent::resetCache($ids);

    // The parent only clears the entity-keyed static cache. For revisionable
    // entity types a 'preload' hook implementation can swap in a non-default
    // revision, which is served from the static revision cache. Invalidate that
    // too so the reloaded entity reflects the persisted state rather than any
    // in-memory modifications.
    if ($this->entityType->isStaticallyCacheable() && $this->entityType->isRevisionable()) {
      $revision_cache_tag = "{$this->entityTypeId}:{$id}:revisions";
      $this->memoryCache->invalidateTags([$revision_cache_tag]);
    }

    // Gather entities from a 'preload' hook. This hook can be used by modules
    // that need, for example, to return a different revision than the default
    // one for revisionable entity types.
+8 −0
Original line number Diff line number Diff line
@@ -470,6 +470,14 @@ public function testNonDefaultRevision(): void {
    // Since the preloaded entity is written back into the default and revision
    // static cache, the two objects are the same.
    $this->assertSame($revision, $loaded);

    // Modifying the swapped-in revision in memory without saving must not
    // affect loadUnchanged(), which has to return the persisted non-default
    // revision rather than the in-memory modification.
    $loaded->setName('Unsaved in-memory edit');
    $unchanged = $storage->loadUnchanged($entity->id());
    $this->assertEquals($expected_revision_id, $unchanged->getRevisionId());
    $this->assertEquals('New name', $unchanged->getName());
  }

}