diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php index 3f66100581..428ce8971d 100644 --- a/core/modules/content_moderation/src/EntityOperations.php +++ b/core/modules/content_moderation/src/EntityOperations.php @@ -3,6 +3,7 @@ namespace Drupal\content_moderation; use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity; +use Drupal\Core\Database\Connection; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; @@ -55,6 +56,13 @@ class EntityOperations implements ContainerInjectionInterface { protected $bundleInfo; /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + + /** * Constructs a new EntityOperations object. * * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info @@ -67,13 +75,16 @@ class EntityOperations implements ContainerInjectionInterface { * The revision tracker. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info * The entity bundle information service. + * @param \Drupal\Core\Database\Connection $database + * The database connection. */ - public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, RevisionTrackerInterface $tracker, EntityTypeBundleInfoInterface $bundle_info) { + public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, RevisionTrackerInterface $tracker, EntityTypeBundleInfoInterface $bundle_info, Connection $database) { $this->moderationInfo = $moderation_info; $this->entityTypeManager = $entity_type_manager; $this->formBuilder = $form_builder; $this->tracker = $tracker; $this->bundleInfo = $bundle_info; + $this->database = $database; } /** @@ -85,7 +96,8 @@ public static function create(ContainerInterface $container) { $container->get('entity_type.manager'), $container->get('form_builder'), $container->get('content_moderation.revision_tracker'), - $container->get('entity_type.bundle.info') + $container->get('entity_type.bundle.info'), + $container->get('database') ); } @@ -145,6 +157,25 @@ public function entityUpdate(EntityInterface $entity) { $this->updateOrCreateFromEntity($entity); $this->setLatestRevision($entity); } + + if ($entity->getEntityTypeId() == 'workflow') { + $deleted_states = array_diff_key($entity->original->getStates(), $entity->getStates()); + /** @var \Drupal\content_moderation\ContentModerationState $deleted_state */ + foreach ($deleted_states as $deleted_state) { + $new_state = $deleted_state->isPublishedState() ? 'published' : 'draft'; + $content_moderation_state_type = $this->entityTypeManager->getDefinition('content_moderation_state'); + $this->database + ->update($content_moderation_state_type->getDataTable()) + ->condition('moderation_state', $deleted_state->id()) + ->fields(['moderation_state' => $new_state]) + ->execute(); + $this->database + ->update($content_moderation_state_type->getRevisionDataTable()) + ->condition('moderation_state', $deleted_state->id()) + ->fields(['moderation_state' => $new_state]) + ->execute(); + } + } } /** diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index ddbb3aec8b..939b453df7 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -58,11 +58,6 @@ class ContentModeration extends WorkflowTypeFormBase implements ContainerFactory protected $moderationInfo; /** - * @var \Drupal\Core\Database\Connection - */ - protected $database; - - /** * Constructs a ContentModeration object. * * @param array $configuration @@ -75,15 +70,12 @@ 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, Connection $database) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, ModerationInformationInterface $moderation_info) { 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; } /** @@ -96,8 +88,7 @@ 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('database') + $container->get('content_moderation.moderation_information') ); } @@ -141,25 +132,6 @@ 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) { /** @var \Drupal\content_moderation\ContentModerationState $state */ $is_required_state = isset($state) ? in_array($state->id(), $this->getRequiredStates(), TRUE) : FALSE;