diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 4270a1fb539e333854e3626fb6e68d6c70909fa8..b5263639e2f76d97c75d7b0ebeaa5b64ed902eb3 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -153,7 +153,7 @@ protected function updateModeratedEntity($moderation_state_id) { // Change the entity's default revision flag and the publishing status only // if the new workflow state is a valid one. - if ($workflow->getTypePlugin()->hasState($moderation_state_id)) { + if ($workflow && $workflow->getTypePlugin()->hasState($moderation_state_id)) { /** @var \Drupal\content_moderation\ContentModerationState $current_state */ $current_state = $workflow->getTypePlugin()->getState($moderation_state_id); diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index a0d46da428c722958239c18cede9036a552d63ba..6281ab82bfa0f18603bb42a08e8647911a380963 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -99,4 +99,29 @@ public function testModerationStateChanges() { $this->assertFalse($this->testNode->isDefaultRevision()); } + /** + * Test updating the state for an entity without a workflow. + */ + public function testEntityWithNoWorkflow() { + $node_type = NodeType::create([ + 'type' => 'example_no_workflow', + ]); + $node_type->save(); + $test_node = Node::create([ + 'type' => 'example_no_workflow', + 'title' => 'Test node with no workflow', + ]); + $test_node->save(); + + /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */ + $content_moderation_info = \Drupal::service('content_moderation.moderation_information'); + $workflow = $content_moderation_info->getWorkflowForEntity($test_node); + $this->assertNull($workflow); + + $this->assertTrue($test_node->isPublished()); + $test_node->moderation_state->setValue('draft'); + // The entity is still published because there is not a workflow. + $this->assertTrue($test_node->isPublished()); + } + }