diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index fa2a1f0..e2955b0 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -128,22 +128,28 @@ public function onChange($delta) { $content_moderation_info = \Drupal::service('content_moderation.moderation_information'); $workflow = $content_moderation_info->getWorkflowForEntity($entity); - /** @var \Drupal\content_moderation\ContentModerationState $current_state */ - $current_state = $workflow->getState($entity->moderation_state->value); - - // This entity is default if it is new, a new translation, the default - // revision state, or the default revision is not published. - $update_default_revision = $entity->isNew() - || $entity->isNewTranslation() - || $current_state->isDefaultRevisionState() - || !$content_moderation_info->isDefaultRevisionPublished($entity); - - $entity->isDefaultRevision($update_default_revision); - - // Update publishing status if it can be updated and if it needs updating. - $published_state = $current_state->isPublishedState(); - if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) { - $published_state ? $entity->setPublished() : $entity->setUnpublished(); + $current_state_id = $this->list[0]->value; + + // Change the entity's default revision flag and the publishing status only + // if the new workflow state is a valid one. + if ($workflow->hasState($current_state_id)) { + /** @var \Drupal\content_moderation\ContentModerationState $current_state */ + $current_state = $workflow->getState($current_state_id); + + // This entity is default if it is new, a new translation, the default + // revision state, or the default revision is not published. + $update_default_revision = $entity->isNew() + || $entity->isNewTranslation() + || $current_state->isDefaultRevisionState() + || !$content_moderation_info->isDefaultRevisionPublished($entity); + + $entity->isDefaultRevision($update_default_revision); + + // Update publishing status if it can be updated and if it needs updating. + $published_state = $current_state->isPublishedState(); + if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) { + $published_state ? $entity->setPublished() : $entity->setUnpublished(); + } } parent::onChange($delta); diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php index 00a3379..5fe22ad 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php @@ -177,7 +177,7 @@ public function testBasicModeration($entity_type_id) { } // Set an invalid moderation state. - $this->setExpectedException(\InvalidArgumentException::class); + $this->setExpectedException(EntityStorageException::class); $entity->moderation_state->value = 'foobar'; $entity->save(); }