diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 3effe8b3c2..87484e88b4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -748,7 +748,7 @@ public function onChange($name) { // 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()) { + if ($key === 'revision' && $this->getRevisionId() == $this->getLoadedRevisionId() && !$this->isNew()) { $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 863f95b332..bf8bcb337f 100644 --- a/core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php +++ b/core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php @@ -256,6 +256,16 @@ public function testNewRevisionRevert() { $this->assertNull($entity->getRevisionId()); $this->assertEquals($revision_id, $entity->getLoadedRevisionId()); $this->assertTrue($entity->isNewRevision()); + + // Check that calling setNewRevision() on a new entity without a revision ID + // and then with a revision ID does not unset the revision ID. + $entity = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']); + $entity->set('revision_id', NULL); + $entity->set('revision_id', 5); + $this->assertTrue($entity->isNewRevision()); + $entity->setNewRevision(); + $this->assertEquals(5, $entity->get('revision_id')->value); + } }