diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index 4a416312ea..20a88f2224 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -5,6 +5,7 @@ use Drupal\Component\Serialization\Json; use Drupal\content_moderation\ModerationInformationInterface; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityPublishedInterface; @@ -57,6 +58,11 @@ class ContentModeration extends WorkflowTypeFormBase implements ContainerFactory protected $moderationInfo; /** + * @var \Drupal\Core\Database\Connection + */ + protected $database; + + /** * Constructs a ContentModeration object. * * @param array $configuration @@ -69,12 +75,15 @@ class ContentModeration extends WorkflowTypeFormBase implements ContainerFactory * The entity type manager. * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info * Moderation information service. + * @param \Drupal\Core\Database\Connection $database + * The database connection. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, ModerationInformationInterface $moderation_info) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, ModerationInformationInterface $moderation_info, Connection $database) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; $this->entityTypeBundleInfo = $entity_type_bundle_info; $this->moderationInfo = $moderation_info; + $this->database = $database; } /** @@ -87,7 +96,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('entity_type.manager'), $container->get('entity_type.bundle.info'), - $container->get('content_moderation.moderation_information') + $container->get('content_moderation.moderation_information'), + $container->get('database') ); } @@ -129,6 +139,25 @@ public function decorateState(StateInterface $state) { } /** + * @inheritDoc + */ + public function deleteState($state_id) { + $new_state = $this->configuration['states'][$state_id]['published'] ? 'published' : 'draft'; + $content_moderation_state_type = $this->entityTypeManager->getDefinition('content_moderation_state'); + $this->database + ->update($content_moderation_state_type->getDataTable()) + ->condition('moderation_state', $state_id) + ->fields(['moderation_state' => $new_state]) + ->execute(); + $this->database + ->update($content_moderation_state_type->getRevisionDataTable()) + ->condition('moderation_state', $state_id) + ->fields(['moderation_state' => $new_state]) + ->execute(); + parent::deleteState($state_id); + } + + /** * {@inheritdoc} */ public function buildStateConfigurationForm(FormStateInterface $form_state, WorkflowInterface $workflow, StateInterface $state = NULL) { diff --git a/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php b/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php new file mode 100644 index 0000000000..31421888d0 --- /dev/null +++ b/core/modules/content_moderation/tests/src/Kernel/DeleteWorkflowStateTest.php @@ -0,0 +1,121 @@ +installSchema('node', 'node_access'); + $this->installEntitySchema('node'); + $this->installEntitySchema('user'); + $this->installEntitySchema('content_moderation_state'); + $this->installConfig('content_moderation'); + + $this->entityTypeManager = $this->container->get('entity_type.manager'); + } + + /** + * Test deleting a node's moderation state. + */ + function testNodeModeration() { + $node_type = NodeType::create([ + 'type' => 'example', + ]); + $node_type->save(); + + $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', + ]); + $node->setUnpublished()->save(); + $this->assertEquals('draft', $node->moderation_state->value); + $this->assertFalse($node->isPublished()); + + $node->moderation_state->value = 'test'; + $node->save(); + $this->assertEquals('test', $node->moderation_state->value); + $this->assertTrue($node->isPublished()); + + $workflow->deleteState('test'); + $workflow->save(); + + $loaded_node = Node::load($node->id()); + $this->assertEquals('published', $loaded_node->moderation_state->value); + $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()); + } + +}