3 namespace Drupal\locale;
5 use Drupal\Core\Config\ExtensionInstallStorage;
6 use Drupal\Core\Config\StorageInterface;
7 use Drupal\language\ConfigurableLanguageManagerInterface;
10 * Provides access to default configuration for locale integration.
12 * Allows unified access to default configuration from one of three sources:
13 * - Required default configuration (config/install/*)
14 * - Optional default configuration (config/optional/*)
15 * - Predefined languages mocked as default configuration (list defined in
16 * LocaleConfigManagerInterface::getStandardLanguageList())
18 * These sources are considered equal in terms of how locale module interacts
19 * with them for translation. Their translatable source strings are exposed
20 * for interface translation and participate in remote translation updates.
22 class LocaleDefaultConfigStorage {
25 * The storage instance for reading configuration data.
27 * @var \Drupal\Core\Config\StorageInterface
29 protected $configStorage;
32 * The language manager.
34 * @var \Drupal\language\ConfigurableLanguageManagerInterface
36 protected $languageManager;
39 * The storage instance for reading required default configuration data.
41 * @var \Drupal\Core\Config\StorageInterface
43 protected $requiredInstallStorage;
46 * The storage instance for reading optional default configuration data.
48 * @var \Drupal\Core\Config\StorageInterface
50 protected $optionalInstallStorage;
53 * Constructs a LocaleDefaultConfigStorage.
55 * @param \Drupal\Core\Config\StorageInterface $config_storage
56 * The storage object to use for reading configuration data.
57 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
58 * The language manager.
60 public function __construct(StorageInterface $config_storage, ConfigurableLanguageManagerInterface $language_manager, $install_profile) {
61 $this->configStorage = $config_storage;
62 $this->languageManager = $language_manager;
64 $this->requiredInstallStorage = new ExtensionInstallStorage($this->configStorage, ExtensionInstallStorage::CONFIG_INSTALL_DIRECTORY, ExtensionInstallStorage::DEFAULT_COLLECTION, TRUE, $install_profile);
65 $this->optionalInstallStorage = new ExtensionInstallStorage($this->configStorage, ExtensionInstallStorage::CONFIG_OPTIONAL_DIRECTORY, ExtensionInstallStorage::DEFAULT_COLLECTION, TRUE, $install_profile);
69 * Read a configuration from install storage or default languages.
72 * Configuration object name.
75 * Configuration data from install storage or default language.
77 public function read($name) {
78 if ($this->requiredInstallStorage->exists($name)) {
79 return $this->requiredInstallStorage->read($name);
81 elseif ($this->optionalInstallStorage->exists($name)) {
82 return $this->optionalInstallStorage->read($name);
84 elseif (strpos($name, 'language.entity.') === 0) {
85 // Simulate default languages as if they were shipped as default
87 $langcode = str_replace('language.entity.', '', $name);
88 $predefined_languages = $this->languageManager->getStandardLanguageList();
89 if (isset($predefined_languages[$langcode])) {
90 $data = $this->configStorage->read($name);
91 $data['label'] = $predefined_languages[$langcode][0];
98 * Return the list of configuration in install storage and current languages.
101 * List of configuration in install storage and current languages.
103 public function listAll() {
104 $languages = $this->predefinedConfiguredLanguages();
107 $this->requiredInstallStorage->listAll(),
108 $this->optionalInstallStorage->listAll(),
115 * Get all configuration names and folders for a list of modules or themes.
117 * @param string $type
118 * Type of components: 'module' | 'theme' | 'profile'
120 * Array of theme or module names.
123 * Configuration names provided by that component. In case of language
124 * module this list is extended with configured languages that have
125 * predefined names as well.
127 public function getComponentNames($type, array $list) {
128 $names = array_unique(
130 array_keys($this->requiredInstallStorage->getComponentNames($type, $list)),
131 array_keys($this->optionalInstallStorage->getComponentNames($type, $list))
134 if ($type == 'module' && in_array('language', $list)) {
135 $languages = $this->predefinedConfiguredLanguages();
136 $names = array_unique(array_merge($names, $languages));
142 * Compute the list of configuration names that match predefined languages.
145 * The list of configuration names that match predefined languages.
147 protected function predefinedConfiguredLanguages() {
148 $names = $this->configStorage->listAll('language.entity.');
149 $predefined_languages = $this->languageManager->getStandardLanguageList();
150 foreach ($names as $id => $name) {
151 $langcode = str_replace('language.entity.', '', $name);
152 if (!isset($predefined_languages[$langcode])) {
156 return array_values($names);