diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 7c0a458..6beb172 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -266,6 +266,7 @@ public function setNewRevision($value = TRUE) { if ($value && !$this->newRevision) { // When saving a new revision, set any existing revision ID to NULL so as // to ensure that a new revision will actually be created. + $this->original_revision = $this->getRevisionId(); $this->set($this->getEntityType()->getKey('revision'), NULL); // Make sure that the flag tracking which translations are affected by the diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index f583121..edc9fda 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -427,7 +427,13 @@ protected function doPreSave(EntityInterface $entity) { // Load the original entity, if any. if ($id_exists && !isset($entity->original)) { - $entity->original = $this->loadUnchanged($id); + if (isset($entity->original_revision)) { + $entity->original = $this->loadRevision($entity->original_revision); + unset($entity->original_revision); + } + else { + $entity->original = $this->loadUnchanged($id); + } } // Allow code to run before saving. diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 453bdf1..241df61 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -399,6 +399,13 @@ function entity_test_entity_test_insert($entity) { } /** + * Implements hook_entity_update(). + */ +function entity_test_entity_update(EntityInterface $entity) { + \Drupal::state()->set('entity_test.original', $entity->original); +} + +/** * Implements hook_entity_field_access(). * * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess() diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalPropertyTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalPropertyTest.php new file mode 100644 index 0000000..8f85745 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalPropertyTest.php @@ -0,0 +1,50 @@ +installEntitySchema('entity_test_mulrev'); + + } + + public function testOriginalProperty() { + $entity = EntityTestMulRev::create(); + $entity->save(); + + $loaded = EntityTestMulRev::load($entity->id()); + $loaded->setNewRevision(TRUE); + $loaded->save(); + + /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */ + $original = \Drupal::state()->get('entity_test.original'); + $this->assertEquals($entity->getRevisionId(), $original->getRevisionId()); + $this->assertNotEquals($loaded->getRevisionId(), $original->getRevisionId()); + + $loaded->set('name', 'dublin'); + $loaded->save(); + $original2 = \Drupal::state()->get('entity_test.original'); + $this->assertEquals($loaded->getRevisionId(), $original2->getRevisionId()); + + } +} diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php index cb73533..bc5965b 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php @@ -215,7 +215,7 @@ public function testIsNewRevision() { ->method('hasKey') ->with('revision') ->will($this->returnValue(TRUE)); - $this->entityType->expects($this->at(5)) + $this->entityType->expects($this->at(6)) ->method('getKey') ->with('revision') ->will($this->returnValue('revision_id'));