diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index aed081a..c03266f 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -266,7 +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->original_revision = $this->getRevisionId(); $this->set($this->getEntityType()->getKey('revision'), NULL); // Make sure that the flag tracking which translations are affected by the @@ -285,6 +285,25 @@ public function setNewRevision($value = TRUE) { /** * {@inheritdoc} */ + public function getOriginalRevisionId() { + if (isset($this->original_revision)) { + return $this->original_revision; + } + } + + /** + * {@inheritdoc} + */ + public function loadUnchangedRevision() { + $storage = $this->entityTypeManager() + ->getStorage($this->entityTypeId); + $storage->resetCache([$this->id()]); + return $storage->loadRevision($this->getOriginalRevisionId()); + } + + /** + * {@inheritdoc} + */ public function isNewRevision() { return $this->newRevision || ($this->getEntityType()->hasKey('revision') && !$this->getRevisionId()); } diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index b82017a..f583121 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -430,12 +430,6 @@ protected function doPreSave(EntityInterface $entity) { $entity->original = $this->loadUnchanged($id); } - // Load the previous revision, if any. - if (isset($entity->previous_revision) && !isset($entity->previous)) { - $entity->previous = $this->loadRevision($entity->previous_revision); - unset($entity->previous_revision); - } - // Allow code to run before saving. $entity->preSave($this); $this->invokeHook('presave', $entity); @@ -481,7 +475,6 @@ protected function doPostSave(EntityInterface $entity, $update) { $entity->setOriginalId($entity->id()); unset($entity->original); - unset($entity->previous); } /** diff --git a/core/lib/Drupal/Core/Entity/RevisionableInterface.php b/core/lib/Drupal/Core/Entity/RevisionableInterface.php index 14690e3..bb56770 100644 --- a/core/lib/Drupal/Core/Entity/RevisionableInterface.php +++ b/core/lib/Drupal/Core/Entity/RevisionableInterface.php @@ -61,4 +61,22 @@ public function isDefaultRevision($new_value = NULL); */ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record); + /** + * Gets the revision identifier of the original entity revision. + * + * @return + * The revision identifier of the original entity revision, or NULL if no + * revision identifier. + */ + public function getOriginalRevisionId(); + + /** + * Loads the unchanged revision entity object. + * + * @return + * The entity object for the unchanged revision, or NULL if no unchanged + * revision. + */ + public function loadUnchangedRevision(); + } 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 54a3659..453bdf1 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -399,15 +399,6 @@ function entity_test_entity_test_insert($entity) { } /** - * Implements hook_entity_update(). - */ -function entity_test_entity_update(EntityInterface $entity) { - if (isset($entity->previous)) { - \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/EntityOriginalRevisionTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php index 3de8902..68d8d75 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php @@ -36,8 +36,10 @@ public function testPreviousProperty() { $loaded->setNewRevision(TRUE); $loaded->save(); - /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_previous */ $this->assertEquals($entity->getRevisionId(), $loaded->getOriginalRevisionId()); $this->assertNotEquals($loaded->getRevisionId(), $loaded->getOriginalRevisionId()); + + $this->assertEquals($entity->getRevisionId(), $loaded->loadUnchangedRevision()->getRevisionId()); + $this->assertNotEquals($loaded->getRevisionId(), $loaded->loadUnchangedRevision()->getRevisionId()); } }