diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php index 8906eba..b8b0262 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstaller.php +++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php @@ -212,6 +212,33 @@ public function installOptionalConfig(StorageInterface $storage = NULL, $depende } /** + * {@inheritdoc} + */ + public function installNewConfig($type, $name, array $config_names) { + + $config_not_found = $config_names; + foreach ([InstallStorage::CONFIG_INSTALL_DIRECTORY, InstallStorage::CONFIG_OPTIONAL_DIRECTORY] as $location) { + $config_path = $this->drupalGetPath($type, $name) . '/' . $location; + $storage = new NewConfigStorage($config_path, StorageInterface::DEFAULT_COLLECTION, $config_names); + + // Check for existing configuration. + $existing = $this->findPreExistingConfiguration($storage); + if (!empty($existing)) { + throw PreExistingConfigException::create($name, $existing); + } + + $config_not_found = array_diff($config_names, $storage->listAll()); + + // Install the new configuration. + $this->installOptionalConfig($storage); + } + + if (!empty($config_not_found)) { + throw new ConfigNameException('Could not find the following configurations: ' . implode(', ', $config_not_found)); + } + } + + /** * Gets configuration data from the provided storage to create. * * @param StorageInterface $storage diff --git a/core/lib/Drupal/Core/Config/ConfigInstallerInterface.php b/core/lib/Drupal/Core/Config/ConfigInstallerInterface.php index ac7ca09..df68c62 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstallerInterface.php +++ b/core/lib/Drupal/Core/Config/ConfigInstallerInterface.php @@ -58,6 +58,23 @@ public function installDefaultConfig($type, $name); public function installOptionalConfig(StorageInterface $storage = NULL, $dependency = []); /** + * Installs new configuration for a given extension. + * + * This is only intended to be used with configuration that is added after a + * module has already been installed. + * + * @param string $type + * The extension type; e.g., 'module' or 'theme'. + * @param string $name + * The name of the module or theme to install default configuration for. + * @param array $config_names + * An array of configuration names to import. + * + * @throws \Drupal\Core\Config\PreExistingConfigException + */ + public function installNewConfig($type, $name, array $config_names); + + /** * Installs all default configuration in the specified collection. * * The function is useful if the site needs to respond to an event that has diff --git a/core/lib/Drupal/Core/Config/NewConfigStorage.php b/core/lib/Drupal/Core/Config/NewConfigStorage.php new file mode 100644 index 0000000..d2450e9 --- /dev/null +++ b/core/lib/Drupal/Core/Config/NewConfigStorage.php @@ -0,0 +1,60 @@ +configNames = $config_names; + } + + /** + * {@inheritdoc} + */ + public function createCollection($collection) { + return new static( + $this->directory, + $collection, + $this->configNames + ); + } + + /** + * {@inheritdoc} + * + * Additionally filter by self::$configNames(). + */ + public function listAll($prefix = '') { + return array_intersect($this->configNames, parent::listAll($prefix)); + } + +} + diff --git a/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php b/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php index 3eff9a8..f520385 100644 --- a/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php +++ b/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\Core\ProxyClass\Config\ConfigInstaller. + * Contains \Drupal\Core\ProxyClass\Config\ConfigInstaller. */ /** @@ -91,6 +91,14 @@ public function installOptionalConfig(\Drupal\Core\Config\StorageInterface $stor /** * {@inheritdoc} */ + public function installNewConfig($type, $name, array $config_names) + { + return $this->lazyLoadItself()->installNewConfig($type, $name, $config_names); + } + + /** + * {@inheritdoc} + */ public function installCollectionDefaultConfig($collection) { return $this->lazyLoadItself()->installCollectionDefaultConfig($collection);