diff --git a/core/lib/Drupal/Core/Config/ConfigEvents.php b/core/lib/Drupal/Core/Config/ConfigEvents.php index 0ba9075..082a0dd 100644 --- a/core/lib/Drupal/Core/Config/ConfigEvents.php +++ b/core/lib/Drupal/Core/Config/ConfigEvents.php @@ -99,6 +99,22 @@ const IMPORT = 'config.importer.import'; /** + * Name of the event fired when installing configuration to target storage. + * + * This event allows modules to perform additional actions when configuration + * is installer. The event listener method receives a + * \Drupal\Core\Config\ConfigInstallerEvent instance. + * + * @Event + * + * @see \Drupal\Core\Config\ConfigInstallerEvent + * @see \Drupal\Core\Config\ConfigInstaller::createConfiguration(). + * + * @var string + */ + const INSTALL = 'config.install'; + + /** * Name of event fired to collect information on all config collections. * * This event allows modules to add to the list of configuration collections diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php index 8065613..44f70d3 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstaller.php +++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php @@ -239,6 +239,7 @@ protected function createConfiguration($collection, array $config_to_install) { else { $new_config->save(); } + $this->eventDispatcher->dispatch(ConfigEvents::INSTALL, new ConfigInstallerEvent($name, $collection)); } } diff --git a/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php b/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php index 8286813..88d7996 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php +++ b/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php @@ -2,58 +2,60 @@ /** * @file - * Contains \Drupal\Core\Config\ConfigImporterEvent. + * Contains \Drupal\Core\Config\ConfigInstallerEvent. */ namespace Drupal\Core\Config; use Symfony\Component\EventDispatcher\Event; -class ConfigImporterEvent extends Event { +class ConfigInstallerEvent extends Event { + /** - * Configuration import object. + * Configuration name. * - * @var \Drupal\Core\Config\ConfigImporter + * @var string */ - protected $configImporter; + protected $name; /** - * Constructs ConfigImporterEvent. + * Collection name. * - * @param \Drupal\Core\Config\ConfigImporter $config_importer - * A config import object to notify listeners about. + * @var string */ - public function __construct(ConfigImporter $config_importer) { - $this->configImporter = $config_importer; - } + protected $collection; /** - * Gets the config import object. + * Constructs ConfigInstallerEvent. * - * @return \Drupal\Core\Config\ConfigImporter - * The ConfigImporter object. + * @param string $name + * Configuration name. + * @param string $collection + * Collection name. */ - public function getConfigImporter() { - return $this->configImporter; + public function __construct($name, $collection) { + $this->name = $name; + $this->collection = $collection; } /** - * Gets the list of changes that will be imported. - * - * @param string $op - * (optional) A change operation. Either delete, create or update. If - * supplied the returned list will be limited to this operation. - * @param string $collection - * (optional) The collection to get the changelist for. Defaults to the - * default collection. + * Gets the config name. * - * @return array - * An array of config changes that are yet to be imported. + * @return string + * The configuration name. + */ + public function getName() { + return $this->name; + } + + /** + * Gets the config collection name. * - * @see \Drupal\Core\Config\StorageComparerInterface::getChangelist() + * @return string + * The collection name. */ - public function getChangelist($op = NULL, $collection = StorageInterface::DEFAULT_COLLECTION) { - return $this->configImporter->getStorageComparer()->getChangelist($op, $collection); + public function getCollection() { + return $this->collection; } } diff --git a/core/modules/locale/locale.services.yml b/core/modules/locale/locale.services.yml index 27cef2c..95683b3 100644 --- a/core/modules/locale/locale.services.yml +++ b/core/modules/locale/locale.services.yml @@ -22,6 +22,6 @@ services: - { name: stream_wrapper, scheme: translations } locale.config_subscriber: class: Drupal\locale\LocaleConfigSubscriber - arguments: ['@locale.storage', '@config.factory', '@locale.config.typed'] + arguments: ['@locale.storage', '@config.factory', '@locale.config.typed', '@language_manager'] tags: - { name: event_subscriber } diff --git a/core/modules/locale/src/LocaleConfigSubscriber.php b/core/modules/locale/src/LocaleConfigSubscriber.php index f0cf416..94e3edb 100644 --- a/core/modules/locale/src/LocaleConfigSubscriber.php +++ b/core/modules/locale/src/LocaleConfigSubscriber.php @@ -7,7 +7,11 @@ namespace Drupal\locale; use Drupal\Core\Config\Config; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\ConfigInstallerEvent; +use Drupal\Core\Config\StorageInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\TypedData\TraversableTypedDataInterface; use Drupal\language\Config\LanguageConfigOverride; use Drupal\language\Config\LanguageConfigOverrideCrudEvent; @@ -49,6 +53,13 @@ class LocaleConfigSubscriber implements EventSubscriberInterface { protected $localeConfigManager; /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** * Constructs a LocaleConfigSubscriber. * * @param \Drupal\locale\StringStorageInterface $string_storage @@ -58,21 +69,49 @@ class LocaleConfigSubscriber implements EventSubscriberInterface { * @param \Drupal\locale\LocaleConfigManager $locale_config_manager * The typed configuration manager. */ - public function __construct(StringStorageInterface $string_storage, ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager) { + public function __construct(StringStorageInterface $string_storage, ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager, LanguageManagerInterface $language_manager) { $this->stringStorage = $string_storage; $this->configFactory = $config_factory; $this->localeConfigManager = $locale_config_manager; + $this->languageManager = $language_manager; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { - $events[LanguageConfigOverrideEvents::SAVE_OVERRIDE] = 'onSave'; - $events[LanguageConfigOverrideEvents::DELETE_OVERRIDE] = 'onDelete'; + $events[LanguageConfigOverrideEvents::SAVE_OVERRIDE] = 'onOverrideSave'; + $events[LanguageConfigOverrideEvents::DELETE_OVERRIDE] = 'onOverrideDelete'; + $events[ConfigEvents::INSTALL] = 'onConfigInstall'; return $events; } + /** + * Updates just installed default configuration to reflect site language. + * + * @param \Drupal\Core\Config\ConfigInstallerEvent $event + */ + public function onConfigInstall(ConfigInstallerEvent $event) { + $site_langcode = $this->languageManager->getDefaultLanguage()->getId(); + // Should only rewrite default configuration if the site is not English by + // default. + if ($site_langcode != 'en') { + $collection = $event->getCollection(); + // Should only rewrite default configuration in the default collection. + if ($collection == StorageInterface::DEFAULT_COLLECTION) { + $name = $event->getName(); + $config = $this->configFactory->getEditable($name); + $default_langcode = $config->get('langcode') ?: 'en'; + // Should only rewrite default configuration if it is in English. + if ($default_langcode == 'en') { + // Just need to change the language code of the active configuration. + // LocaleConfigManager is later responsible to merge in the proper + // translations for each translatable piece and keep it up to date. + $config->set('langcode', $site_langcode)->save(); + } + } + } + } /** * Updates the translation strings when shipped configuration is saved. @@ -80,7 +119,7 @@ public static function getSubscribedEvents() { * @param \Drupal\language\Config\LanguageConfigOverrideCrudEvent $event * The language configuration event. */ - public function onSave(LanguageConfigOverrideCrudEvent $event) { + public function onOverrideSave(LanguageConfigOverrideCrudEvent $event) { // Do not mark strings as customized when community translations are being // imported. if ($this->localeConfigManager->isUpdatingConfigTranslations()) { @@ -99,7 +138,7 @@ public function onSave(LanguageConfigOverrideCrudEvent $event) { * @param \Drupal\language\Config\LanguageConfigOverrideCrudEvent $event * The language configuration event. */ - public function onDelete(LanguageConfigOverrideCrudEvent $event) { + public function onOverrideDelete(LanguageConfigOverrideCrudEvent $event) { if ($this->localeConfigManager->isUpdatingConfigTranslations()) { $callable = [$this, 'deleteTranslation']; } @@ -201,7 +240,7 @@ protected function saveTranslation($source_value, $langcode, $translation_value * (optional) The translation string value. If omitted, a customized string * with the source value will be saved. * - * @see \Drupal\locale\LocaleConfigSubscriber::onDelete() + * @see \Drupal\locale\LocaleConfigSubscriber::onOverrideDelete() */ protected function saveCustomizedTranslation($source_value, $langcode, $translation_value = NULL) { if ($translation = $this->getTranslation($source_value, $langcode, TRUE)) { @@ -225,7 +264,7 @@ protected function saveCustomizedTranslation($source_value, $langcode, $translat * @param string $langcode * The language code of the translation. * - * @see \Drupal\locale\LocaleConfigSubscriber::onDelete() + * @see \Drupal\locale\LocaleConfigSubscriber::onOverrideDelete() */ protected function deleteTranslation($source_value, $langcode) { if ($translation = $this->getTranslation($source_value, $langcode, FALSE)) { diff --git a/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php b/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php new file mode 100644 index 0000000..88d7996 --- /dev/null +++ b/core/lib/Drupal/Core/Config/ConfigInstallerEvent.php @@ -0,0 +1,61 @@ +name = $name; + $this->collection = $collection; + } + + /** + * Gets the config name. + * + * @return string + * The configuration name. + */ + public function getName() { + return $this->name; + } + + /** + * Gets the config collection name. + * + * @return string + * The collection name. + */ + public function getCollection() { + return $this->collection; + } + +}