Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / src / EventSubscriber / ConfigImportSubscriber.php
1 <?php
2
3 namespace Drupal\content_moderation\EventSubscriber;
4
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;
10
11 /**
12  * Check moderation states are not being used before updating workflow config.
13  */
14 class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase {
15
16   /**
17    * The config manager.
18    *
19    * @var \Drupal\Core\Config\ConfigManagerInterface
20    */
21   protected $configManager;
22
23   /**
24    * The entity type manager.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27    */
28   protected $entityTypeManager;
29
30   /**
31    * Constructs the event subscriber.
32    *
33    * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
34    *   The config manager
35    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
36    *   The entity type manager.
37    */
38   public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
39     $this->configManager = $config_manager;
40     $this->entityTypeManager = $entity_type_manager;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
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()
54               ->getSourceStorage()
55               ->read($unprocessed_configuration);
56             $workflow_config = $event->getConfigImporter()
57               ->getStorageComparer()
58               ->getTargetStorage()
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()]));
65               }
66             }
67           }
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()]));
71             }
72           }
73         }
74       }
75     }
76   }
77
78   /**
79    * Get the workflow entity object from the configuration name.
80    *
81    * @param string $config_name
82    *   The configuration object name.
83    *
84    * @return \Drupal\workflows\WorkflowInterface|null
85    *   A workflow entity object. NULL if no matching entity is found.
86    */
87   protected function getWorkflow($config_name) {
88     $entity_type_id = $this->configManager->getEntityTypeIdByName($config_name);
89     if ($entity_type_id !== 'workflow') {
90       return;
91     }
92
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);
97   }
98
99 }