diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 7c0a458..e63cafd 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -16,7 +16,7 @@ * * @ingroup entity_api */ -abstract class ContentEntityBase extends Entity implements \IteratorAggregate, ContentEntityInterface { +abstract class ContentEntityBase extends Entity implements \IteratorAggregate, ContentEntityInterface, OriginalRevisionIdInterface { /** * Status code identifying a removed translation. @@ -163,6 +163,13 @@ protected $validationRequired = FALSE; /** + * The original revision id before the new revision was set. + * + * @var int + */ + protected $originalRevisionId; + + /** * {@inheritdoc} */ public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) { @@ -230,6 +237,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(); } /** @@ -284,6 +294,20 @@ public function setNewRevision($value = TRUE) { /** * {@inheritdoc} */ + public function getOriginalRevisionId() { + return $this->originalRevisionId; + } + + /** + * {@inheritdoc} + */ + public function resetOriginalRevisionId() { + $this->originalRevisionId = $this->getRevisionId(); + } + + /** + * {@inheritdoc} + */ public function isNewRevision() { return $this->newRevision || ($this->getEntityType()->hasKey('revision') && !$this->getRevisionId()); } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index afdce40..ef936cc 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -305,6 +305,9 @@ protected function doPostSave(EntityInterface $entity, $update) { // The revision is stored, it should no longer be marked as new now. if ($this->entityType->isRevisionable()) { + if ($entity instanceof OriginalRevisionIdInterface) { + $entity->resetOriginalRevisionId(); + } $entity->setNewRevision(FALSE); } } diff --git a/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php b/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php new file mode 100644 index 0000000..111f642 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/OriginalRevisionIdInterface.php @@ -0,0 +1,25 @@ +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 new file mode 100644 index 0000000..1694dad --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php @@ -0,0 +1,77 @@ +installEntitySchema('entity_test_mulrev'); + + } + + public function testPreviousProperty() { + // Create a basic EntityTestMulRev entity and save it. + $entity = EntityTestMulRev::create(); + $entity->save(); + + // Load the created entity and create a new revision. + $loaded = EntityTestMulRev::load($entity->id()); + $loaded->setNewRevision(TRUE); + + // Before saving the original revision ID should be the same as the created + // entity, not the same as the loaded entity (which won't have a revision id + // yet). + $this->assertEquals($entity->getRevisionId(), $loaded->getOriginalRevisionId()); + $this->assertNotEquals($loaded->getRevisionId(), $loaded->getOriginalRevisionId()); + $this->assertTrue($loaded->getRevisionId() === NULL); + + $loaded->save(); + + // In hook_update the original revision id was stored in state. This should + // be the same as we had before calling $loaded->save(). + /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */ + $originalRevisionId = \Drupal::state()->get('entity_test.originalRevisionId'); + $this->assertEquals($entity->getRevisionId(), $originalRevisionId); + $this->assertNotEquals($loaded->getRevisionId(), $originalRevisionId); + + // The revision ID and original revision ID should be different for the two + // versions of the entity, but the same for a saved entity. + $this->assertNotEquals($loaded->getRevisionId(), $entity->getRevisionId()); + $this->assertNotEquals($loaded->getOriginalRevisionId(), $entity->getOriginalRevisionId()); + $this->assertEquals($entity->getRevisionId(), $entity->getOriginalRevisionId()); + $this->assertEquals($loaded->getRevisionId(), $loaded->getOriginalRevisionId()); + + // Make a change to the loaded entity. + $loaded->set('name', 'dublin'); + + // The revision id and original revision id should still be the same. + $this->assertEquals($loaded->getRevisionId(), $loaded->getOriginalRevisionId()); + + $loaded->save(); + + // After saving the original revision id set in hook_update, and returned + // from the entity should be the same and the entity's revision id. + $originalRevisionId2 = \Drupal::state()->get('entity_test.originalRevisionId'); + $this->assertEquals($loaded->getRevisionId(), $originalRevisionId2); + $this->assertEquals($loaded->getRevisionId(), $loaded->getOriginalRevisionId()); + } +}