You are here

protected function ConfigInstaller::createConfiguration in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/ConfigInstaller.php \Drupal\Core\Config\ConfigInstaller::createConfiguration()
  2. 9 core/lib/Drupal/Core/Config/ConfigInstaller.php \Drupal\Core\Config\ConfigInstaller::createConfiguration()

Creates configuration in a collection based on the provided list.

Parameters

string $collection: The configuration collection.

array $config_to_create: An array of configuration data to create, keyed by name.

File

core/lib/Drupal/Core/Config/ConfigInstaller.php, line 327

Class

ConfigInstaller

Namespace

Drupal\Core\Config

Code

protected function createConfiguration($collection, array $config_to_create) {

  // Order the configuration to install in the order of dependencies.
  if ($collection == StorageInterface::DEFAULT_COLLECTION) {
    $dependency_manager = new ConfigDependencyManager();
    $config_names = $dependency_manager
      ->setData($config_to_create)
      ->sortAll();
  }
  else {
    $config_names = array_keys($config_to_create);
  }
  foreach ($config_names as $name) {

    // Allow config factory overriders to use a custom configuration object if
    // they are responsible for the collection.
    $overrider = $this->configManager
      ->getConfigCollectionInfo()
      ->getOverrideService($collection);
    if ($overrider) {
      $new_config = $overrider
        ->createConfigObject($name, $collection);
    }
    else {
      $new_config = new Config($name, $this
        ->getActiveStorages($collection), $this->eventDispatcher, $this->typedConfig);
    }
    if ($config_to_create[$name] !== FALSE) {

      // Add a hash to configuration created through the installer so it is
      // possible to know if the configuration was created by installing an
      // extension and to track which version of the default config was used.
      if (!$this
        ->isSyncing() && $collection == StorageInterface::DEFAULT_COLLECTION) {
        $config_to_create[$name] = [
          '_core' => [
            'default_config_hash' => Crypt::hashBase64(serialize($config_to_create[$name])),
          ],
        ] + $config_to_create[$name];
      }
      $new_config
        ->setData($config_to_create[$name]);
    }
    if ($collection == StorageInterface::DEFAULT_COLLECTION && ($entity_type = $this->configManager
      ->getEntityTypeIdByName($name))) {

      // If we are syncing do not create configuration entities. Pluggable
      // configuration entities can have dependencies on modules that are
      // not yet enabled. This approach means that any code that expects
      // default configuration entities to exist will be unstable after the
      // module has been enabled and before the config entity has been
      // imported.
      if ($this
        ->isSyncing()) {
        continue;
      }

      /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
      $entity_storage = $this->configManager
        ->getEntityTypeManager()
        ->getStorage($entity_type);
      $id = $entity_storage
        ->getIDFromConfigName($name, $entity_storage
        ->getEntityType()
        ->getConfigPrefix());

      // It is possible that secondary writes can occur during configuration
      // creation. Updates of such configuration are allowed.
      if ($this
        ->getActiveStorages($collection)
        ->exists($name)) {
        $entity = $entity_storage
          ->load($id);
        $entity = $entity_storage
          ->updateFromStorageRecord($entity, $new_config
          ->get());
      }
      else {
        $entity = $entity_storage
          ->createFromStorageRecord($new_config
          ->get());
      }
      if ($entity
        ->isInstallable()) {
        $entity
          ->trustData()
          ->save();
        if ($id !== $entity
          ->id()) {
          trigger_error(sprintf('The configuration name "%s" does not match the ID "%s"', $name, $entity
            ->id()), E_USER_WARNING);
        }
      }
    }
    else {
      $new_config
        ->save(TRUE);
    }
  }
}