You are here

public function ConfigInstaller::installOptionalConfig in Zircon Profile 8

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

Installs optional configuration.

Optional configuration is only installed if:

  • the configuration does not exist already.
  • it's a configuration entity.
  • its dependencies can be met.

Parameters

\Drupal\Core\Config\StorageInterface: (optional) The configuration storage to search for optional configuration. If not provided, all enabled extension's optional configuration directories will be searched.

array $dependency: (optional) If set, ensures that the configuration being installed has this dependency. The format is dependency type as the key ('module', 'theme', or 'config') and the dependency name as the value ('book', 'bartik', 'views.view.frontpage').

Overrides ConfigInstallerInterface::installOptionalConfig

1 call to ConfigInstaller::installOptionalConfig()
ConfigInstaller::installDefaultConfig in core/lib/Drupal/Core/Config/ConfigInstaller.php
Installs the default configuration of a given extension.

File

core/lib/Drupal/Core/Config/ConfigInstaller.php, line 158
Contains \Drupal\Core\Config\ConfigInstaller.

Class

ConfigInstaller

Namespace

Drupal\Core\Config

Code

public function installOptionalConfig(StorageInterface $storage = NULL, $dependency = []) {
  $profile = $this
    ->drupalGetProfile();
  $optional_profile_config = [];
  if (!$storage) {

    // Search the install profile's optional configuration too.
    $storage = new ExtensionInstallStorage($this
      ->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION, TRUE);

    // The extension install storage ensures that overrides are used.
    $profile_storage = NULL;
  }
  elseif (isset($profile)) {

    // Creates a profile storage to search for overrides.
    $profile_install_path = $this
      ->drupalGetPath('module', $profile) . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
    $profile_storage = new FileStorage($profile_install_path, StorageInterface::DEFAULT_COLLECTION);
    $optional_profile_config = $profile_storage
      ->listAll();
  }
  else {

    // Profile has not been set yet. For example during the first steps of the
    // installer or during unit tests.
    $profile_storage = NULL;
  }
  $enabled_extensions = $this
    ->getEnabledExtensions();
  $existing_config = $this
    ->getActiveStorages()
    ->listAll();
  $list = array_unique(array_merge($storage
    ->listAll(), $optional_profile_config));
  $list = array_filter($list, function ($config_name) use ($existing_config) {

    // Only list configuration that:
    // - does not already exist
    // - is a configuration entity (this also excludes config that has an
    //   implicit dependency on modules that are not yet installed)
    return !in_array($config_name, $existing_config) && $this->configManager
      ->getEntityTypeIdByName($config_name);
  });
  $all_config = array_merge($existing_config, $list);
  $config_to_create = $storage
    ->readMultiple($list);

  // Check to see if the corresponding override storage has any overrides or
  // new configuration that can be installed.
  if ($profile_storage) {
    $config_to_create = $profile_storage
      ->readMultiple($list) + $config_to_create;
  }
  foreach ($config_to_create as $config_name => $data) {

    // Exclude configuration where its dependencies cannot be met.
    if (!$this
      ->validateDependencies($config_name, $data, $enabled_extensions, $all_config)) {
      unset($config_to_create[$config_name]);
    }
    elseif (!empty($dependency)) {

      // Create a light weight dependency object to check dependencies.
      $config_entity = new ConfigEntityDependency($config_name, $data);
      if (!$config_entity
        ->hasDependency(key($dependency), reset($dependency))) {
        unset($config_to_create[$config_name]);
      }
    }
  }
  if (!empty($config_to_create)) {
    $this
      ->createConfiguration(StorageInterface::DEFAULT_COLLECTION, $config_to_create, TRUE);
  }
}