diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 7c0a458..aed081a 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->previous_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..880bc9b 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -430,6 +430,12 @@ protected function doPreSave(EntityInterface $entity) { $entity->original = $this->loadUnchanged($id); } + // Load the previous revision, if any. + if ($id_exists && !isset($entity->previous)) { + $entity->previous = $this->loadRevision($entity->original_revision); + unset($entity->original_revision); + } + // Allow code to run before saving. $entity->preSave($this); $this->invokeHook('presave', $entity); @@ -475,6 +481,7 @@ protected function doPostSave(EntityInterface $entity, $update) { $entity->setOriginalId($entity->id()); unset($entity->original); + unset($entity->previous); } /** 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..46ddd90 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.previous', $entity->previous); +} + +/** * Implements hook_entity_field_access(). * * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess() diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityPreviousPropertyTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityPreviousPropertyTest.php new file mode 100644 index 0000000..d9f6632 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityPreviousPropertyTest.php @@ -0,0 +1,49 @@ +installEntitySchema('entity_test_mulrev'); + + } + + public function testPreviousProperty() { + $entity = EntityTestMulRev::create(); + $entity->save(); + + $loaded = EntityTestMulRev::load($entity->id()); + $loaded->setNewRevision(TRUE); + $loaded->save(); + + /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_previous */ + $previous = \Drupal::state()->get('entity_test.previous'); + $this->assertEquals($entity->getRevisionId(), $previous->getRevisionId()); + $this->assertNotEquals($loaded->getRevisionId(), $previous->getRevisionId()); + + $loaded->set('name', 'dublin'); + $loaded->save(); + $previous2 = \Drupal::state()->get('entity_test.previous'); + $this->assertEquals($loaded->getRevisionId(), $previous2->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'));