3 namespace Drupal\content_moderation\EventSubscriber;
5 use Drupal\Core\Config\ConfigImporterEvent;
6 use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase;
7 use Drupal\Core\Config\ConfigManagerInterface;
8 use Drupal\Core\Config\Entity\ConfigEntityStorage;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
12 * Check moderation states are not being used before updating workflow config.
14 class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase {
19 * @var \Drupal\Core\Config\ConfigManagerInterface
21 protected $configManager;
24 * The entity type manager.
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28 protected $entityTypeManager;
31 * Constructs the event subscriber.
33 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
35 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
36 * The entity type manager.
38 public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
39 $this->configManager = $config_manager;
40 $this->entityTypeManager = $entity_type_manager;
46 public function onConfigImporterValidate(ConfigImporterEvent $event) {
47 foreach (['update', 'delete'] as $op) {
48 $unprocessed_configurations = $event->getConfigImporter()->getUnprocessedConfiguration($op);
49 foreach ($unprocessed_configurations as $unprocessed_configuration) {
50 if ($workflow = $this->getWorkflow($unprocessed_configuration)) {
51 if ($op === 'update') {
52 $original_workflow_config = $event->getConfigImporter()
53 ->getStorageComparer()
55 ->read($unprocessed_configuration);
56 $workflow_config = $event->getConfigImporter()
57 ->getStorageComparer()
59 ->read($unprocessed_configuration);
60 $diff = array_diff_key($workflow_config['type_settings']['states'], $original_workflow_config['type_settings']['states']);
61 foreach (array_keys($diff) as $state_id) {
62 $state = $workflow->getTypePlugin()->getState($state_id);
63 if ($workflow->getTypePlugin()->workflowStateHasData($workflow, $state)) {
64 $event->getConfigImporter()->logError($this->t('The moderation state @state_label is being used, but is not in the source storage.', ['@state_label' => $state->label()]));
68 if ($op === 'delete') {
69 if ($workflow->getTypePlugin()->workflowHasData($workflow)) {
70 $event->getConfigImporter()->logError($this->t('The workflow @workflow_label is being used, and cannot be deleted.', ['@workflow_label' => $workflow->label()]));
79 * Get the workflow entity object from the configuration name.
81 * @param string $config_name
82 * The configuration object name.
84 * @return \Drupal\workflows\WorkflowInterface|null
85 * A workflow entity object. NULL if no matching entity is found.
87 protected function getWorkflow($config_name) {
88 $entity_type_id = $this->configManager->getEntityTypeIdByName($config_name);
89 if ($entity_type_id !== 'workflow') {
93 /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
94 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
95 $entity_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
96 return $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id);