diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 7c0a458..7c0c711 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -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()) { @@ -266,6 +273,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->originalRevisionId = $this->getRevisionId(); $this->set($this->getEntityType()->getKey('revision'), NULL); // Make sure that the flag tracking which translations are affected by the @@ -284,6 +292,29 @@ public function setNewRevision($value = TRUE) { /** * {@inheritdoc} */ + public function getOriginalRevisionId() { + $revison_id = $this->originalRevisionId; + if (!$revison_id) { + $revison_id = $this->getRevisionId(); + } + return $revison_id; + } + + /** + * {@inheritdoc} + */ + public function loadUnchangedRevision() { + if ($this->getOriginalRevisionId()) { + $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/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/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php new file mode 100644 index 0000000..68d8d75 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityOriginalRevisionTest.php @@ -0,0 +1,45 @@ +installEntitySchema('entity_test_mulrev'); + + } + + public function testPreviousProperty() { + $entity = EntityTestMulRev::create(); + $entity->save(); + + $loaded = EntityTestMulRev::load($entity->id()); + $loaded->setNewRevision(TRUE); + $loaded->save(); + + $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()); + } +} 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'));