diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index e242544..7cfb217 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -384,6 +384,17 @@ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $reco /** * {@inheritdoc} */ + public function save() { + if ($this->getEntityType()->isRevisionable()) { + // Make sure the loaded revision id is set before saving. + $this->updateLoadedRevisionId(); + } + return parent::save(); + } + + /** + * {@inheritdoc} + */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 52bb740..84015bf 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -297,14 +297,6 @@ protected function doPreSave(EntityInterface $entity) { protected function doPostSave(EntityInterface $entity, $update) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - // Update the loaded revision ID for a new entity before calling the post - // save hooks, to avoid unexpected behavior when resaving the entity. - if ($this->entityType->isRevisionable()) { - if (!$update) { - $entity->updateLoadedRevisionId(); - } - } - if ($update && $this->entityType->isTranslatable()) { $this->invokeTranslationHooks($entity); } @@ -313,9 +305,7 @@ protected function doPostSave(EntityInterface $entity, $update) { // The revision is stored, it should no longer be marked as new now. if ($this->entityType->isRevisionable()) { - if ($update) { - $entity->updateLoadedRevisionId(); - } + $entity->updateLoadedRevisionId(); $entity->setNewRevision(FALSE); } } diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 404dd97..cf1a63c 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -414,6 +414,16 @@ function entity_test_entity_test_insert($entity) { } /** + * Implements hook_entity_insert(). + */ +function entity_test_entity_insert(EntityInterface $entity) { + if ($entity->getEntityTypeId() == 'entity_test_mulrev' && $entity->label() == 'EntityLoadedRevisionTest') { + $entity->setNewRevision(FALSE); + $entity->save(); + } +} + +/** * Implements hook_entity_update(). */ function entity_test_entity_update(EntityInterface $entity) { diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityLoadedRevisionTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityLoadedRevisionTest.php index 9fedf26..26c163a 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityLoadedRevisionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityLoadedRevisionTest.php @@ -126,5 +126,12 @@ public function testLoadedRevisionId() { $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId()); $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId()); + // Create an entity which will be saved again in entity_test_entity_insert(). + $entity2 = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']); + $entity2->save(); + $loadedRevisionId3 = \Drupal::state()->get('entity_test.loadedRevisionId'); + $this->assertEquals($entity2->getLoadedRevisionId(), $loadedRevisionId3); + $this->assertEquals($entity2->getRevisionId(), $entity2->getLoadedRevisionId()); + } }