function ConfigInstaller::createConfiguration

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/ConfigInstaller.php \Drupal\Core\Config\ConfigInstaller::createConfiguration()
  2. 8.9.x core/lib/Drupal/Core/Config/ConfigInstaller.php \Drupal\Core\Config\ConfigInstaller::createConfiguration()
  3. 10 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.

4 calls to ConfigInstaller::createConfiguration()
ConfigInstaller::installCollectionDefaultConfig in core/lib/Drupal/Core/Config/ConfigInstaller.php
Installs all default configuration in the specified collection.
ConfigInstaller::installDefaultConfig in core/lib/Drupal/Core/Config/ConfigInstaller.php
Installs the default configuration of a given extension.
ConfigInstaller::installOptionalConfig in core/lib/Drupal/Core/Config/ConfigInstaller.php
Installs optional configuration.
RecipeConfigInstaller::installRecipeConfig in core/lib/Drupal/Core/Recipe/RecipeConfigInstaller.php

File

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

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()) {
                    throw new \LogicException(sprintf('The configuration name "%s" does not match the ID "%s"', $name, $entity->id()));
                }
            }
        }
        else {
            $new_config->save(TRUE);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.