diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index b5fccbd3694..7c0a4581de4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -1032,6 +1032,11 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C $this->clearTranslationCache(); $translations = $this->translations; $this->translations = &$translations; + + // Ensure the enforceIsNew property is actually cloned by overwriting the + // original reference with one pointing to a copy of it. + $enforce_is_new = $this->enforceIsNew; + $this->enforceIsNew = &$enforce_is_new; } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php index 2a05b948fb4..cca7eb12910 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php @@ -62,4 +62,31 @@ class ContentEntityCloneTest extends EntityKernelTestBase { } } + /** + * Tests that the flag for enforcing a new entity is not shared. + */ + public function testEnforceIsNewOnClonedEntityTranslation() { + // Create a test entity. + $entity = EntityTestMul::create([ + 'name' => $this->randomString(), + 'language' => 'en', + ]); + $entity->save(); + $entity_translation = $entity->addTranslation('de'); + $entity->save(); + + // The entity is not new anymore. + $this->assertFalse($entity_translation->isNew()); + + // The clone should not be new as well. + $clone = clone $entity_translation; + $this->assertFalse($clone->isNew()); + + // After enforcing the clone to be new only it should be flagged as new, + // but the original entity should not be flagged as new. + $clone->enforceIsNew(); + $this->assertTrue($clone->isNew()); + $this->assertFalse($entity_translation->isNew()); + } + }