diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 5376644c548cabbdcb0ab8980d926b754253bbf8..3effe8b3c25947fe37d14ea11c864e5b39d24939 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -745,6 +745,12 @@ public function onChange($name) { elseif (isset($this->translatableEntityKeys[$key][$this->activeLangcode])) { unset($this->translatableEntityKeys[$key][$this->activeLangcode]); } + // If the revision identifier field is being populated with the original + // value, we need to make sure the "new revision" flag is reset + // accordingly. + if ($key === 'revision' && $this->getRevisionId() == $this->getLoadedRevisionId()) { + $this->newRevision = FALSE; + } } } diff --git a/core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php b/core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php index 8b544c4abde37ecc918def341382858beb7e2d17..863f95b332d9dee6d679dea1b546a75388663194 100644 --- a/core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php +++ b/core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php @@ -212,4 +212,50 @@ public function testEntityRevisionParamConverter() { $this->assertNoText('pending revision - en'); } + /** + * Tests manual revert of the revision ID value. + * + * @covers \Drupal\Core\Entity\ContentEntityBase::getRevisionId + * @covers \Drupal\Core\Entity\ContentEntityBase::getLoadedRevisionId + * @covers \Drupal\Core\Entity\ContentEntityBase::setNewRevision + * @covers \Drupal\Core\Entity\ContentEntityBase::isNewRevision + */ + public function testNewRevisionRevert() { + $entity = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']); + $entity->save(); + + // Check that revision ID field is reset while the loaded revision ID is + // preserved when flagging a new revision. + $revision_id = $entity->getRevisionId(); + $entity->setNewRevision(); + $this->assertNull($entity->getRevisionId()); + $this->assertEquals($revision_id, $entity->getLoadedRevisionId()); + $this->assertTrue($entity->isNewRevision()); + + // Check that after manually restoring the original revision ID, the entity + // is stored without creating a new revision. + $key = $entity->getEntityType()->getKey('revision'); + $entity->set($key, $revision_id); + $entity->save(); + $this->assertEquals($revision_id, $entity->getRevisionId()); + $this->assertEquals($revision_id, $entity->getLoadedRevisionId()); + + // Check that manually restoring the original revision ID causes the "new + // revision" state to be reverted. + $entity->setNewRevision(); + $this->assertNull($entity->getRevisionId()); + $this->assertEquals($revision_id, $entity->getLoadedRevisionId()); + $this->assertTrue($entity->isNewRevision()); + $entity->set($key, $revision_id); + $this->assertFalse($entity->isNewRevision()); + $this->assertEquals($revision_id, $entity->getRevisionId()); + $this->assertEquals($revision_id, $entity->getLoadedRevisionId()); + + // Check that flagging a new revision again works correctly. + $entity->setNewRevision(); + $this->assertNull($entity->getRevisionId()); + $this->assertEquals($revision_id, $entity->getLoadedRevisionId()); + $this->assertTrue($entity->isNewRevision()); + } + }