ConfigInstaller::installOptionalConfig

public ConfigInstaller::installOptionalConfig(StorageInterface $storage = NULL, $dependency = [])

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 $storage: (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

File

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

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);
  $all_config = array_combine($all_config, $all_config);
  $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;
  }
  // Sort $config_to_create in the order of the least dependent first.
  $dependency_manager = new ConfigDependencyManager();
  $dependency_manager->setData($config_to_create);
  $config_to_create = array_merge(array_flip($dependency_manager->sortAll()), $config_to_create);

  foreach ($config_to_create as $config_name => $data) {
    // Remove configuration where its dependencies cannot be met.
    $remove = !$this->validateDependencies($config_name, $data, $enabled_extensions, $all_config);
    // If $dependency is defined, remove configuration that does not have a
    // matching dependency.
    if (!$remove && !empty($dependency)) {
      // Create a light weight dependency object to check dependencies.
      $config_entity = new ConfigEntityDependency($config_name, $data);
      $remove = !$config_entity->hasDependency(key($dependency), reset($dependency));
    }

    if ($remove) {
      // Remove from the list of configuration to create.
      unset($config_to_create[$config_name]);
      // Remove from the list of all configuration. This ensures that any
      // configuration that depends on this configuration is also removed.
      unset($all_config[$config_name]);
    }
  }
  if (!empty($config_to_create)) {
    $this->createConfiguration(StorageInterface::DEFAULT_COLLECTION, $config_to_create, TRUE);
  }
}
doc_Drupal
2016-10-29 08:53:55
Comments
Leave a Comment

Please login to continue.