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..138dbef --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.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_original */ + $originalRevisionId = \Drupal::state()->get('entity_test.originalRevisionId'); + $this->assertEquals($entity->getRevisionId(), $originalRevisionId); + $this->assertNotEquals($loaded->getRevisionId(), $originalRevisionId); + + $loaded->set('name', 'dublin'); + $loaded->save(); + $originalRevisionId2 = \Drupal::state()->get('entity_test.originalRevisionId'); + $this->assertEquals($loaded->getRevisionId(), $originalRevisionId2); + } +}