summaryrefslogtreecommitdiffstats
path: root/core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php
diff options
context:
space:
mode:
authorLee Rowlands2017-07-27 23:07:55 (GMT)
committerLee Rowlands2017-07-27 23:07:55 (GMT)
commit832d7695acaff7c557d0a0b607a2676d983b36ac (patch)
tree0441fdcb7db55509ed58610a12f3a329a5878f4d /core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php
parente6cd4b0dc20f9bff638bee6c8c327477f7c38988 (diff)
Issue #2830740 by timmillwood, amateescu, Sam152, alexpott, martin107, plach, catch: Allow workflow types to lock certain changes to workflows once things are in use
Diffstat (limited to 'core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php')
-rw-r--r--core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php96
1 files changed, 96 insertions, 0 deletions
diff --git a/core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php b/core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php
new file mode 100644
index 0000000..bb7d680
--- /dev/null
+++ b/core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php
@@ -0,0 +1,96 @@
+<?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->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\Core\Entity\EntityInterface|null
+ * An entity object. NULL if no matching entity is found.
+ */
+ protected function getWorkflow($config_name) {
+ $entity_type_id = $this->configManager->getEntityTypeIdByName($config_name);
+ /** @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());
+ /** @var \Drupal\workflows\WorkflowInterface $workflow */
+ return $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id);
+ }
+
+}