class PreExistingConfigException

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/PreExistingConfigException.php \Drupal\Core\Config\PreExistingConfigException
  2. 8.9.x core/lib/Drupal/Core/Config/PreExistingConfigException.php \Drupal\Core\Config\PreExistingConfigException
  3. 11.x core/lib/Drupal/Core/Config/PreExistingConfigException.php \Drupal\Core\Config\PreExistingConfigException

An exception thrown if configuration with the same name already exists.

Hierarchy

Expanded class hierarchy of PreExistingConfigException

7 files declare their use of PreExistingConfigException
ConfigInstallTest.php in core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php
ConfigInstallWebTest.php in core/modules/config/tests/src/Functional/ConfigInstallWebTest.php
ModulesEnabledTrait.php in core/modules/system/src/Form/ModulesEnabledTrait.php
ModulesListConfirmForm.php in core/modules/system/src/Form/ModulesListConfirmForm.php
ModulesListForm.php in core/modules/system/src/Form/ModulesListForm.php

... See full list

File

core/lib/Drupal/Core/Config/PreExistingConfigException.php, line 10

Namespace

Drupal\Core\Config
View source
class PreExistingConfigException extends ConfigException {
    
    /**
     * A list of configuration objects that already exist in active configuration.
     *
     * @var array
     */
    protected $configObjects = [];
    
    /**
     * The name of the module that is being installed.
     *
     * @var string
     */
    protected $extension;
    
    /**
     * Gets the list of configuration objects that already exist.
     *
     * @return array
     *   A list of configuration objects that already exist in active
     *   configuration keyed by collection.
     */
    public function getConfigObjects() {
        return $this->configObjects;
    }
    
    /**
     * Gets the name of the extension that is being installed.
     *
     * @return string
     *   The name of the extension that is being installed.
     */
    public function getExtension() {
        return $this->extension;
    }
    
    /**
     * Creates an exception for an extension and a list of configuration objects.
     *
     * @param $extension
     *   The name of the extension that is being installed.
     * @param array $config_objects
     *   A list of configuration objects that already exist in active
     *   configuration, keyed by config collection.
     *
     * @return $this
     */
    public static function create($extension, array $config_objects) {
        $message = new FormattableMarkup('Configuration objects (@config_names) provided by @extension already exist in active configuration', [
            '@config_names' => implode(', ', static::flattenConfigObjects($config_objects)),
            '@extension' => $extension,
        ]);
        $e = new static($message);
        $e->configObjects = $config_objects;
        $e->extension = $extension;
        return $e;
    }
    
    /**
     * Flattens the config object array to a single dimensional list.
     *
     * @param array $config_objects
     *   A list of configuration objects that already exist in active
     *   configuration, keyed by config collection.
     *
     * @return array
     *   A list of configuration objects that have been prefixed with their
     *   collection.
     */
    public static function flattenConfigObjects(array $config_objects) {
        $flat_config_objects = [];
        foreach ($config_objects as $collection => $config_names) {
            $config_names = array_map(function ($config_name) use ($collection) {
                if ($collection != StorageInterface::DEFAULT_COLLECTION) {
                    $config_name = str_replace('.', DIRECTORY_SEPARATOR, $collection) . DIRECTORY_SEPARATOR . $config_name;
                }
                return $config_name;
            }, $config_names);
            $flat_config_objects[] = $config_names;
        }
        return array_merge(...$flat_config_objects);
    }

}

Members

Title Sort descending Modifiers Object type Summary
PreExistingConfigException::$configObjects protected property A list of configuration objects that already exist in active configuration.
PreExistingConfigException::$extension protected property The name of the module that is being installed.
PreExistingConfigException::create public static function Creates an exception for an extension and a list of configuration objects.
PreExistingConfigException::flattenConfigObjects public static function Flattens the config object array to a single dimensional list.
PreExistingConfigException::getConfigObjects public function Gets the list of configuration objects that already exist.
PreExistingConfigException::getExtension public function Gets the name of the extension that is being installed.

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