diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 32394a1..eb1a1b2 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -17,7 +17,7 @@ * * @ingroup entity_api */ -abstract class ContentEntityBase extends Entity implements \IteratorAggregate, ContentEntityInterface, TranslationStatusInterface { +abstract class ContentEntityBase extends Entity implements \IteratorAggregate, ContentEntityInterface, TranslationStatusInterface, OriginalRevisionIdInterface { /** * The plain data values of the contained fields. @@ -226,6 +226,9 @@ public function __construct(array $values, $entity_type, $bundle = FALSE, $trans } } } + // Store the original revision identfier the entity has been loaded with to + // keep it safe from changes. + $this->originalRevisionId = $this->getRevisionId(); } /** @@ -262,7 +265,6 @@ 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->originalRevisionId = $this->getRevisionId(); $this->set($this->getEntityType()->getKey('revision'), NULL); // Make sure that the flag tracking which translations are affected by the @@ -282,23 +284,14 @@ public function setNewRevision($value = TRUE) { * {@inheritdoc} */ public function getOriginalRevisionId() { - $revison_id = $this->originalRevisionId; - if (!$revison_id) { - $revison_id = $this->getRevisionId(); - } - return $revison_id; + return $this->originalRevisionId; } /** * {@inheritdoc} */ - public function loadUnchangedRevision() { - if ($this->getOriginalRevisionId()) { - $storage = $this->entityTypeManager() - ->getStorage($this->entityTypeId); - $storage->resetCache([$this->id()]); - return $storage->loadRevision($this->getOriginalRevisionId()); - } + public function resetOriginalRevisionId() { + $this->originalRevisionId = $this->getRevisionId(); } /** diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index afdce40..127c188 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -305,6 +305,7 @@ protected function doPostSave(EntityInterface $entity, $update) { // The revision is stored, it should no longer be marked as new now. if ($this->entityType->isRevisionable()) { + $entity->resetOriginalRevisionId(); $entity->setNewRevision(FALSE); } } diff --git a/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php b/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php index e69de29..4cacc4e 100644 --- a/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php +++ b/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php @@ -0,0 +1,30 @@ +set('entity_test.originalRevisionId', $entity->getOriginalRevisionId()); +} + +/** * 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 68d8d75..138dbef 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php @@ -36,10 +36,14 @@ public function testPreviousProperty() { $loaded->setNewRevision(TRUE); $loaded->save(); - $this->assertEquals($entity->getRevisionId(), $loaded->getOriginalRevisionId()); - $this->assertNotEquals($loaded->getRevisionId(), $loaded->getOriginalRevisionId()); + /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */ + $originalRevisionId = \Drupal::state()->get('entity_test.originalRevisionId'); + $this->assertEquals($entity->getRevisionId(), $originalRevisionId); + $this->assertNotEquals($loaded->getRevisionId(), $originalRevisionId); - $this->assertEquals($entity->getRevisionId(), $loaded->loadUnchangedRevision()->getRevisionId()); - $this->assertNotEquals($loaded->getRevisionId(), $loaded->loadUnchangedRevision()->getRevisionId()); + $loaded->set('name', 'dublin'); + $loaded->save(); + $originalRevisionId2 = \Drupal::state()->get('entity_test.originalRevisionId'); + $this->assertEquals($loaded->getRevisionId(), $originalRevisionId2); } }