ConfigImportSubscriber.php in Drupal 10
File
core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php
View source
<?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;
class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase {
protected $configManager;
protected $entityTypeManager;
public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->configManager = $config_manager;
$this->entityTypeManager = $entity_type_manager;
}
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(),
]));
}
}
}
}
}
}
protected function getWorkflow($config_name) {
$entity_type_id = $this->configManager
->getEntityTypeIdByName($config_name);
if ($entity_type_id !== 'workflow') {
return;
}
$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);
}
}