You are here

public function ExcludedModulesEventSubscriber::onConfigTransformImport in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/EventSubscriber/ExcludedModulesEventSubscriber.php \Drupal\Core\EventSubscriber\ExcludedModulesEventSubscriber::onConfigTransformImport()

Transform the storage which is used to import the configuration.

Make sure excluded modules are not uninstalled by adding them and their config to the storage when importing configuration.

Parameters

\Drupal\Core\Config\StorageTransformEvent $event: The transformation event.

File

core/lib/Drupal/Core/EventSubscriber/ExcludedModulesEventSubscriber.php, line 74

Class

ExcludedModulesEventSubscriber
The event subscriber preventing excluded modules to be exported.

Namespace

Drupal\Core\EventSubscriber

Code

public function onConfigTransformImport(StorageTransformEvent $event) {
  $storage = $event
    ->getStorage();
  if (!$storage
    ->exists('core.extension')) {

    // If the core.extension config is not present there is nothing to do.
    // This means that probably the storage is empty or non-functional.
    return;
  }
  foreach (array_merge([
    StorageInterface::DEFAULT_COLLECTION,
  ], $this->activeStorage
    ->getAllCollectionNames()) as $collectionName) {
    $collection = $storage
      ->createCollection($collectionName);
    $activeCollection = $this->activeStorage
      ->createCollection($collectionName);
    foreach ($this
      ->getDependentConfigNames() as $configName) {
      if (!$collection
        ->exists($configName) && $activeCollection
        ->exists($configName)) {

        // Make sure the config is not removed if it exists.
        $collection
          ->write($configName, $activeCollection
          ->read($configName));
      }
    }
  }
  $extension = $storage
    ->read('core.extension');
  $existing = $this->activeStorage
    ->read('core.extension');
  $modules = $extension['module'];
  foreach ($this
    ->getExcludedModules() as $module) {
    if (array_key_exists($module, $existing['module'])) {

      // Set the modules weight from the active store.
      $modules[$module] = $existing['module'][$module];
    }
  }

  // Sort the extensions.
  $extension['module'] = module_config_sort($modules);

  // Set the modified extension.
  $storage
    ->write('core.extension', $extension);
}