You are here

public function ConfigInstaller::installDefaultConfig in Drupal 8

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

Installs the default configuration of a given extension.

When an extension is installed, it searches all the default configuration directories for all other extensions to locate any configuration with its name prefix. For example, the Node module provides the frontpage view as a default configuration file: core/modules/node/config/install/views.view.frontpage.yml When the Views module is installed after the Node module is already enabled, the frontpage view will be installed.

Additionally, the default configuration directory for the extension being installed is searched to discover if it contains default configuration that is owned by other enabled extensions. So, the frontpage view will also be installed when the Node module is installed after Views.

Parameters

string $type: The extension type; e.g., 'module' or 'theme'.

string $name: The name of the module or theme to install default configuration for.

Overrides ConfigInstallerInterface::installDefaultConfig

See also

\Drupal\Core\Config\ExtensionInstallStorage

File

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

Class

ConfigInstaller

Namespace

Drupal\Core\Config

Code

public function installDefaultConfig($type, $name) {
  $extension_path = $this
    ->drupalGetPath($type, $name);

  // Refresh the schema cache if the extension provides configuration schema
  // or is a theme.
  if (is_dir($extension_path . '/' . InstallStorage::CONFIG_SCHEMA_DIRECTORY) || $type == 'theme') {
    $this->typedConfig
      ->clearCachedDefinitions();
  }
  $default_install_path = $this
    ->getDefaultConfigDirectory($type, $name);
  if (is_dir($default_install_path)) {
    if (!$this
      ->isSyncing()) {
      $storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);
      $prefix = '';
    }
    else {

      // The configuration importer sets the source storage on the config
      // installer. The configuration importer handles all of the
      // configuration entity imports. We only need to ensure that simple
      // configuration is created when the extension is installed.
      $storage = $this
        ->getSourceStorage();
      $prefix = $name . '.';
    }

    // Gets profile storages to search for overrides if necessary.
    $profile_storages = $this
      ->getProfileStorages($name);

    // Gather information about all the supported collections.
    $collection_info = $this->configManager
      ->getConfigCollectionInfo();
    foreach ($collection_info
      ->getCollectionNames() as $collection) {
      $config_to_create = $this
        ->getConfigToCreate($storage, $collection, $prefix, $profile_storages);
      if ($name == $this
        ->drupalGetProfile()) {

        // If we're installing a profile ensure simple configuration that
        // already exists is excluded as it will have already been written.
        // This means that if the configuration is changed by something else
        // during the install it will not be overwritten again.
        $existing_configuration = array_filter($this
          ->getActiveStorages($collection)
          ->listAll(), function ($config_name) {
          return !$this->configManager
            ->getEntityTypeIdByName($config_name);
        });
        $config_to_create = array_diff_key($config_to_create, array_flip($existing_configuration));
      }
      if (!empty($config_to_create)) {
        $this
          ->createConfiguration($collection, $config_to_create);
      }
    }
  }

  // During a drupal installation optional configuration is installed at the
  // end of the installation process.
  // @see install_install_profile()
  if (!$this
    ->isSyncing() && !InstallerKernel::installationAttempted()) {
    $optional_install_path = $extension_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
    if (is_dir($optional_install_path)) {

      // Install any optional config the module provides.
      $storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
      $this
        ->installOptionalConfig($storage, '');
    }

    // Install any optional configuration entities whose dependencies can now
    // be met. This searches all the installed modules config/optional
    // directories.
    $storage = new ExtensionInstallStorage($this
      ->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION, FALSE, $this->installProfile);
    $this
      ->installOptionalConfig($storage, [
      $type => $name,
    ]);
  }

  // Reset all the static caches and list caches.
  $this->configFactory
    ->reset();
}