3 namespace Drupal\Core\Config;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\StringTranslation\TranslationInterface;
9 * An exception thrown if configuration has unmet dependencies.
11 class UnmetDependenciesException extends ConfigException {
14 * A list of configuration objects that have unmet dependencies.
16 * The list is keyed by the config object name, and the value is an array of
17 * the missing dependencies:
21 * self::configObjects = [
22 * config_object_name => [
23 * 'missing_dependency_1',
24 * 'missing_dependency_2',
32 protected $configObjects = [];
35 * The name of the extension that is being installed.
42 * Gets the list of configuration objects that have unmet dependencies.
45 * A list of configuration objects that have unmet dependencies, keyed by
46 * object name, with the value being a list of the unmet dependencies.
48 public function getConfigObjects() {
49 return $this->configObjects;
53 * Gets the name of the extension that is being installed.
56 * The name of the extension that is being installed.
58 public function getExtension() {
59 return $this->extension;
63 * Gets a translated message from the exception.
65 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
66 * The string translation service.
70 public function getTranslatedMessage(TranslationInterface $string_translation, $extension) {
71 return $string_translation->translate(
72 'Unable to install %extension due to unmet dependencies: %config_names',
74 '%config_names' => static::formatConfigObjectList($this->configObjects),
75 '%extension' => $extension,
81 * Creates an exception for an extension and a list of configuration objects.
84 * The name of the extension that is being installed.
85 * @param array $config_objects
86 * A list of configuration keyed by configuration name, with unmet
87 * dependencies as the value.
89 * @return \Drupal\Core\Config\PreExistingConfigException
91 public static function create($extension, array $config_objects) {
92 $message = new FormattableMarkup('Configuration objects provided by %extension have unmet dependencies: %config_names',
94 '%config_names' => static::formatConfigObjectList($config_objects),
95 '%extension' => $extension
98 $e = new static($message);
99 $e->configObjects = $config_objects;
100 $e->extension = $extension;
105 * Formats a list of configuration objects.
107 * @param array $config_objects
108 * A list of configuration object names that have unmet dependencies.
111 * The imploded config_objects, formatted in an easy to read string.
113 protected static function formatConfigObjectList(array $config_objects) {
115 foreach ($config_objects as $config_object => $missing_dependencies) {
116 $list[] = $config_object . ' (' . implode(', ', $missing_dependencies) . ')';
118 return implode(', ', $list);