summaryrefslogtreecommitdiffstats
path: root/core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php
blob: 930167f04f23383be87003fd965adca2630e8e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

namespace Drupal\content_moderation\EventSubscriber;

use Drupal\Core\Config\ConfigImporterEvent;
use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Check moderation states are not being used before updating workflow config.
 */
class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase {

  /**
   * The config manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs the event subscriber.
   *
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   The config manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
    $this->configManager = $config_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    foreach (['update', 'delete'] as $op) {
      $unprocessed_configurations = $event->getConfigImporter()->getUnprocessedConfiguration($op);
      foreach ($unprocessed_configurations as $unprocessed_configuration) {
        if ($workflow = $this->getWorkflow($unprocessed_configuration)) {
          if ($op === 'update') {
            $original_workflow_config = $event->getConfigImporter()
              ->getStorageComparer()
              ->getSourceStorage()
              ->read($unprocessed_configuration);
            $workflow_config = $event->getConfigImporter()
              ->getStorageComparer()
              ->getTargetStorage()
              ->read($unprocessed_configuration);
            $diff = array_diff_key($workflow_config['type_settings']['states'], $original_workflow_config['type_settings']['states']);
            foreach (array_keys($diff) as $state_id) {
              $state = $workflow->getTypePlugin()->getState($state_id);
              if ($workflow->getTypePlugin()->workflowStateHasData($workflow, $state)) {
                $event->getConfigImporter()->logError($this->t('The moderation state @state_label is being used, but is not in the source storage.', ['@state_label' => $state->label()]));
              }
            }
          }
          if ($op === 'delete') {
            if ($workflow->getTypePlugin()->workflowHasData($workflow)) {
              $event->getConfigImporter()->logError($this->t('The workflow @workflow_label is being used, and cannot be deleted.', ['@workflow_label' => $workflow->label()]));
            }
          }
        }
      }
    }
  }

  /**
   * Get the workflow entity object from the configuration name.
   *
   * @param string $config_name
   *   The configuration object name.
   *
   * @return \Drupal\workflows\WorkflowInterface|null
   *   A workflow entity object. NULL if no matching entity is found.
   */
  protected function getWorkflow($config_name) {
    $entity_type_id = $this->configManager->getEntityTypeIdByName($config_name);
    if ($entity_type_id !== 'workflow') {
      return;
    }

    /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
    $entity_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
    return $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id);
  }

}