diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index 288ac0e1f0..5c1d827bbf 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -133,11 +133,17 @@ public function decorateState(StateInterface $state) { */ public function deleteState($state_id) { $new_state = $this->configuration['states'][$state_id]['published'] ? 'published' : 'draft'; - $content_moderation_states = $this->entityTypeManager->getStorage('content_moderation_state')->loadByProperties(['moderation_state' => $state_id]); - foreach ($content_moderation_states as $content_moderation_state) { - $content_moderation_state->moderation_state->value = $new_state; - $content_moderation_state->save(); - } + $content_moderation_state_type = $this->entityTypeManager->getDefinition('content_moderation_state'); + \Drupal::database() + ->update($content_moderation_state_type->getDataTable()) + ->condition('moderation_state', $state_id) + ->fields(['moderation_state' => $new_state]) + ->execute(); + \Drupal::database() + ->update($content_moderation_state_type->getRevisionDataTable()) + ->condition('moderation_state', $state_id) + ->fields(['moderation_state' => $new_state]) + ->execute(); parent::deleteState($state_id); } diff --git a/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php b/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php index cd40182e9a..31421888d0 100644 --- a/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php @@ -57,18 +57,25 @@ function testNodeModeration() { $workflow = Workflow::load('editorial'); $workflow->addState('test', 'Test'); + $workflow->addState('unpub_test', 'Unpublished Test'); $workflow->getTypePlugin()->setConfiguration([ 'states' => [ 'test' => [ 'published' => TRUE, 'default_revision' => TRUE, ], + 'unpub_test' => [ + 'published' => FALSE, + 'default_revision' => FALSE, + ], ], ]); $workflow->addTransition('test', 'Test', ['draft'], 'test'); + $workflow->addTransition('unpub_test', 'Unpublished Test', ['draft'], 'unpub_test'); $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example'); $workflow->save(); + // Tests deleting a published moderation state. $node = Node::create([ 'type' => 'example', 'title' => 'Test title', @@ -87,7 +94,28 @@ function testNodeModeration() { $loaded_node = Node::load($node->id()); $this->assertEquals('published', $loaded_node->moderation_state->value); - $this->assertTrue($node->isPublished()); + $this->assertTrue($loaded_node->isPublished()); + + // Tests deleting an unpublished moderation state. + $node2 = Node::create([ + 'type' => 'example', + 'title' => 'Test title 2', + ]); + $node2->setUnpublished()->save(); + $this->assertEquals('draft', $node2->moderation_state->value); + $this->assertFalse($node2->isPublished()); + + $node2->moderation_state->value = 'unpub_test'; + $node2->save(); + $this->assertEquals('unpub_test', $node2->moderation_state->value); + $this->assertFalse($node2->isPublished()); + + $workflow->deleteState('unpub_test'); + $workflow->save(); + + $loaded_node2 = Node::load($node2->id()); + $this->assertEquals('draft', $loaded_node2->moderation_state->value); + $this->assertFalse($loaded_node2->isPublished()); } -} \ No newline at end of file +}