3 namespace Drupal\Core\Config;
5 use Drupal\Component\Render\FormattableMarkup;
8 * An exception thrown if configuration with the same name already exists.
10 class PreExistingConfigException extends ConfigException {
13 * A list of configuration objects that already exist in active configuration.
17 protected $configObjects = [];
20 * The name of the module that is being installed.
27 * Gets the list of configuration objects that already exist.
30 * A list of configuration objects that already exist in active
31 * configuration keyed by collection.
33 public function getConfigObjects() {
34 return $this->configObjects;
38 * Gets the name of the extension that is being installed.
41 * The name of the extension that is being installed.
43 public function getExtension() {
44 return $this->extension;
48 * Creates an exception for an extension and a list of configuration objects.
51 * The name of the extension that is being installed.
52 * @param array $config_objects
53 * A list of configuration objects that already exist in active
54 * configuration, keyed by config collection.
56 * @return \Drupal\Core\Config\PreExistingConfigException
58 public static function create($extension, array $config_objects) {
59 $message = new FormattableMarkup('Configuration objects (@config_names) provided by @extension already exist in active configuration',
61 '@config_names' => implode(', ', static::flattenConfigObjects($config_objects)),
62 '@extension' => $extension,
65 $e = new static($message);
66 $e->configObjects = $config_objects;
67 $e->extension = $extension;
72 * Flattens the config object array to a single dimensional list.
74 * @param array $config_objects
75 * A list of configuration objects that already exist in active
76 * configuration, keyed by config collection.
79 * A list of configuration objects that have been prefixed with their
82 public static function flattenConfigObjects(array $config_objects) {
83 $flat_config_objects = [];
84 foreach ($config_objects as $collection => $config_names) {
85 $config_names = array_map(function ($config_name) use ($collection) {
86 if ($collection != StorageInterface::DEFAULT_COLLECTION) {
87 $config_name = str_replace('.', DIRECTORY_SEPARATOR, $collection) . DIRECTORY_SEPARATOR . $config_name;
91 $flat_config_objects = array_merge($flat_config_objects, $config_names);
93 return $flat_config_objects;