diff --git a/core/includes/config.inc b/core/includes/config.inc index 00c7fa6..71f9f5a 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -4,6 +4,7 @@ use Drupal\Core\Config\FileStorage; use Drupal\Core\Config\NullStorage; use Drupal\Core\Config\StorageInterface; +use Drupal\Core\Language\Language; /** * @file @@ -106,6 +107,21 @@ function config($name) { } /** + * Sets the language on the configuration object factory. + * + * @param Drupal\Core\Language\Language $language + * The language that config objects should have injected in to them. + * + * @return Drupal\Core\Language\Language $language + * The language object that was previously set in the config factory. + */ +function config_factory_set_language(Language $language) { + $current_language = drupal_container()->get('config.factory')->getLanguage(); + drupal_container()->get('config.factory')->setLanguage($language); + return $current_language; +} + +/** * Returns a list of differences between configuration storages. * * @param Drupal\Core\Config\StorageInterface $source_storage @@ -257,11 +273,11 @@ function config_import_invoke_owner(array $config_changes, StorageInterface $sou // handle the configuration change. $handled_by_module = FALSE; if (module_exists($module) && module_hook($module, 'config_import_' . $op)) { - $old_config = new Config($name, $target_storage); + $old_config = new Config($name, $target_storage, language_default()); $old_config->load(); $data = $source_storage->read($name); - $new_config = new Config($name, $target_storage); + $new_config = new Config($name, $target_storage, language_default()); if ($data !== FALSE) { $new_config->setData($data); } diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 6fc2a87..899abf6 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Config; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Language\Language; use Symfony\Component\EventDispatcher\EventDispatcher; /** @@ -65,6 +66,13 @@ class Config { protected $eventDispatcher; /** + * The Language object used to override configuration data. + * + * @var Drupal\Core\Language\Language + */ + protected $language; + + /** * Constructs a configuration object. * * @param string $name @@ -72,12 +80,15 @@ class Config { * @param Drupal\Core\Config\StorageInterface $storage * A storage controller object to use for reading and writing the * configuration data. + * @param Drupal\Core\Language\Language $language + * The Language object used to override configuration data. * @param Symfony\Component\EventDispatcher\EventDispatcher $event_dispatcher * The event dispatcher used to notify subscribers. */ - public function __construct($name, StorageInterface $storage, EventDispatcher $event_dispatcher = NULL) { + public function __construct($name, StorageInterface $storage, Language $language, EventDispatcher $event_dispatcher = NULL) { $this->name = $name; $this->storage = $storage; + $this->language = $language; $this->eventDispatcher = $event_dispatcher ? $event_dispatcher : drupal_container()->get('dispatcher'); } @@ -416,4 +427,14 @@ public function merge(array $data_to_merge) { $this->data = NestedArray::mergeDeepArray(array($this->data, $data_to_merge), TRUE); return $this; } + + /** + * Returns the language object for this Config object. + * + * @return \Drupal\Core\Language\Language + */ + public function getLanguage() { + return $this->language; + } } + diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index ca36ce7..c86a58a 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Config; use Symfony\Component\EventDispatcher\EventDispatcher; +use Drupal\Core\Language\Language; /** * Defines the configuration object factory. @@ -46,10 +47,15 @@ class ConfigFactory { * configuration data. * @param Symfony\Component\EventDispatcher\EventDispatcher * An event dispatcher instance to use for configuration events. + * @param Drupal\Core\Language\Language + * A language object to inject in to configuration objects. */ - public function __construct(StorageInterface $storage, EventDispatcher $event_dispatcher) { + public function __construct(StorageInterface $storage, EventDispatcher $event_dispatcher, Language $language = NULL) { $this->storage = $storage; $this->eventDispatcher = $event_dispatcher; + // @todo: always inject language, once we figure out how to handle language + // and the DIC in early bootstrap phases. + $this->language = isset($language) ? $language : language_default(); } /** @@ -82,8 +88,27 @@ public function get($name) { // @todo The decrease of CPU time is interesting, since that means that // ContainerBuilder involves plenty of function calls (which are known to // be slow in PHP). - $config = new Config($name, $this->storage, $this->eventDispatcher); + $config = new Config($name, $this->storage, $this->language, $this->eventDispatcher); return $config->init(); } + /** + * Set the language to be injected in to Config objects. + * + * @param Language $language + * @return ConfigFactory + */ + public function setLanguage(Language $language) { + $this->language = $language; + return $this; + } + + /** + * Get the language to be injected in to Config objects. + * + * @return Drupal\Core\Language\Language + */ + public function getLanguage() { + return $this->language; + } } diff --git a/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php index 8f0359d..26860e9 100644 --- a/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php @@ -88,6 +88,15 @@ public function onKernelRequestLanguageResolve(GetResponseEvent $event) { // prior to front page and alias resolution. When above is decoupled, also // add 'langcode' (determined from $request only) to $request->attributes. drupal_language_initialize(); + + // Set the negotiated language as the default for Config objects created by + // the ConfigFactory. + $language = drupal_container() + ->get('language_manager') + ->getLanguage(LANGUAGE_TYPE_INTERFACE); + drupal_container() + ->get('config.factory') + ->setLanguage($language); } /** diff --git a/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php b/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php index 3d2fd4a..4ae656d 100644 --- a/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php +++ b/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php @@ -26,7 +26,7 @@ class LocaleConfigSubscriber implements EventSubscriberInterface { */ public function configLoad(ConfigEvent $event) { $config = $event->getConfig(); - $language = language(LANGUAGE_TYPE_INTERFACE); + $language = $config->getLanguage(); $locale_name = $this->getLocaleConfigName($config->getName(), $language); if ($override = $config->getStorage()->read($locale_name)) { $config->setOverride($override); diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 285b9a7..59d9c67 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1893,8 +1893,16 @@ function user_view_multiple($accounts, $view_mode = 'full', $langcode = NULL) { function user_mail($key, &$message, $params) { $langcode = $message['langcode']; $variables = array('user' => $params['account']); + + // Make sure that we get configuration values for the user's language, not + // the site default language. + $config_language = config_factory_set_language(language_load(user_preferred_langcode($params['account']))); + $message['subject'] .= _user_mail_text($key . '.subject', $langcode, $variables); $message['body'][] = _user_mail_text($key . '.body', $langcode, $variables); + + // Set the language back to the site default. + config_factory_set_language($config_language); } /**