You are here

class ConfigImportSubscriber in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  2. 9 core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php \Drupal\content_moderation\EventSubscriber\ConfigImportSubscriber
Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php \Drupal\content_moderation\EventSubscriber\ConfigImportSubscriber

Check moderation states are not being used before updating workflow config.

Hierarchy

Expanded class hierarchy of ConfigImportSubscriber

1 string reference to 'ConfigImportSubscriber'
content_moderation.services.yml in core/modules/content_moderation/content_moderation.services.yml
core/modules/content_moderation/content_moderation.services.yml
1 service uses ConfigImportSubscriber
content_moderation.config_import_subscriber in core/modules/content_moderation/content_moderation.services.yml
Drupal\content_moderation\EventSubscriber\ConfigImportSubscriber

File

core/modules/content_moderation/src/EventSubscriber/ConfigImportSubscriber.php, line 14

Namespace

Drupal\content_moderation\EventSubscriber
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigImportSubscriber::$configManager protected property The config manager.
ConfigImportSubscriber::$entityTypeManager protected property The entity type manager.
ConfigImportSubscriber::getWorkflow protected function Get the workflow entity object from the configuration name.
ConfigImportSubscriber::onConfigImporterValidate public function Checks that the configuration synchronization is valid. Overrides ConfigImportValidateEventSubscriberBase::onConfigImporterValidate
ConfigImportSubscriber::__construct public function Constructs the event subscriber.
ConfigImportValidateEventSubscriberBase::getSubscribedEvents public static function 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.