diff --git a/core/includes/config.inc b/core/includes/config.inc index 0011533..d2070fc 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -2,6 +2,8 @@ use Drupal\Core\Config\Config; use Drupal\Core\Config\FileStorage; +use Drupal\Core\Config\Context\AdminContext; +use Drupal\Core\Config\ContextInterface; use Drupal\Core\Config\NullStorage; use Drupal\Core\Config\StorageInterface; @@ -93,7 +95,7 @@ function config_get_storage_names_with_prefix($prefix = '') { * @code config('book.admin') @endcode will return a configuration object in * which the book module can store its administrative settings. * - * @param $name + * @param string $name * The name of the configuration object to retrieve. The name corresponds to * a configuration file. For @code config('book.admin') @endcode, the config * object returned will contain the contents of book.admin configuration file. @@ -106,6 +108,21 @@ function config($name) { } /** + * Sets the context on the configuration object factory. + * + * @param Drupal\Core\Config\ContextInterface $context + * The configuration context to set on the factory. + * + * @return Drupal\Core\Config\ContextInterface $context + * The configuration context that was previously set in the config factory. + */ +function config_factory_set_context(ContextInterface $context) { + $current_context = drupal_container()->get('config.factory')->getContext(); + drupal_container()->get('config.factory')->setContext($context); + return $current_context; +} + +/** * Returns a list of differences between configuration storages. * * @param Drupal\Core\Config\StorageInterface $source_storage @@ -247,6 +264,8 @@ function config_import() { * @todo Add support for other extension types; e.g., themes etc. */ function config_import_invoke_owner(array $config_changes, StorageInterface $source_storage, StorageInterface $target_storage) { + $target_context = new AdminContext('config.target', $target_storage); + $source_context = new AdminContext('config.target', $source_storage); // Allow modules to take over configuration change operations for // higher-level configuration data. // First pass deleted, then new, and lastly changed configuration, in order to @@ -259,11 +278,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_context); $old_config->load(); $data = $source_storage->read($name); - $new_config = new Config($name, $target_storage); + $new_config = new Config($name, $source_context); if ($data !== FALSE) { $new_config->setData($data); } diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 755c2c1..cafe393 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -327,9 +327,11 @@ function install_begin_request(&$install_state) { $container->register('event_dispatcher', 'Symfony\Component\EventDispatcher\EventDispatcher'); $container->register('config.storage', 'Drupal\Core\Config\InstallStorage'); - $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory') + $container->register('config.context', 'Drupal\Core\Config\Context\ConfigContext') ->addArgument(new Reference('config.storage')) ->addArgument(new Reference('event_dispatcher')); + $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory') + ->addArgument(new Reference('config.context')); drupal_container($container); } diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 783c6b8..bc33c09 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Config; use Drupal\Component\Utility\NestedArray; -use Symfony\Component\EventDispatcher\EventDispatcher; /** * Defines the default configuration object. @@ -58,11 +57,11 @@ class Config { protected $storage; /** - * The event dispatcher used to notify subscribers. + * The configuration context used for this configuration object. * - * @var Symfony\Component\EventDispatcher\EventDispatcher + * @var Drupal\Core\Config\ContextInterface */ - protected $eventDispatcher; + protected $context; /** * Whether the config object has already been loaded. @@ -76,16 +75,13 @@ class Config { * * @param string $name * The name of the configuration object being constructed. - * @param Drupal\Core\Config\StorageInterface $storage - * A storage controller object to use for reading and writing the - * configuration data. - * @param Symfony\Component\EventDispatcher\EventDispatcher $event_dispatcher - * The event dispatcher used to notify subscribers. + * @param Drupal\Core\Config\ContextInterface $context + * The configuration context used for this configuration object. */ - public function __construct($name, StorageInterface $storage, EventDispatcher $event_dispatcher = NULL) { + public function __construct($name, ContextInterface $context) { $this->name = $name; - $this->storage = $storage; - $this->eventDispatcher = $event_dispatcher ? $event_dispatcher : drupal_container()->get('event_dispatcher'); + $this->context = $context; + $this->storage = $context->getStorage(); } /** @@ -432,20 +428,10 @@ public function delete() { } /** - * Retrieve the storage used to load and save this configuration object. - * - * @return \Drupal\Core\Config\StorageInterface - * The configuration storage object. - */ - public function getStorage() { - return $this->storage; - } - - /** * Dispatch a config event. */ protected function notify($config_event_name) { - $this->eventDispatcher->dispatch('config.' . $config_event_name, new ConfigEvent($this)); + $this->context->notify($config_event_name, $this); } /* diff --git a/core/lib/Drupal/Core/Config/ConfigEvent.php b/core/lib/Drupal/Core/Config/ConfigEvent.php index aabd1d8..efe6953 100644 --- a/core/lib/Drupal/Core/Config/ConfigEvent.php +++ b/core/lib/Drupal/Core/Config/ConfigEvent.php @@ -3,9 +3,9 @@ namespace Drupal\Core\Config; use Symfony\Component\EventDispatcher\Event; -use Drupal\Core\Config\Config; class ConfigEvent extends Event { + /** * Configuration object. * @@ -14,10 +14,23 @@ class ConfigEvent extends Event { protected $config; /** - * Constructor. + * Configuration context object. + * + * @var Drupal\Core\Config\ContextInterface + */ + protected $context; + + /** + * Constructs a configuration event object. + * + * @param Drupal\Core\Config\ContextInterface + * Configuration context object. + * @param Drupal\Core\Config\Config + * (optional) Configuration object. */ - public function __construct(Config $config) { + public function __construct(ContextInterface $context, Config $config = NULL) { $this->config = $config; + $this->context = $context; } /** @@ -26,4 +39,14 @@ public function __construct(Config $config) { public function getConfig() { return $this->config; } + + /** + * Get configuration context object. + * + * @return Drupal\Core\Config\ContextInterface + * Configuration context. + */ + public function getContext() { + return $this->context; + } } diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index 00d9773..16fee43 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Config; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Drupal\Core\Config\ContextInterface; /** * Defines the configuration object factory. @@ -17,26 +17,20 @@ * * @see Drupal\Core\Config\Config * - * Each configuration object gets a storage controller object injected, which - * is used for reading and writing the configuration data. + * A configuration context is an object containing parameters that will be + * available to the configuration plug-ins for them to customize the + * configuration data in different ways. * * @see Drupal\Core\Config\StorageInterface */ class ConfigFactory { /** - * A storage controller instance for reading and writing configuration data. + * The default configuration context associated with this factory. * - * @var Drupal\Core\Config\StorageInterface + * @var Drupal\Core\Config\ContextInterface */ - protected $storage; - - /** - * An event dispatcher instance to use for configuration events. - * - * @var Symfony\Component\EventDispatcher\EventDispatcher - */ - protected $eventDispatcher; + protected $context; /** * Cached configuration objects. @@ -48,33 +42,27 @@ class ConfigFactory { /** * Constructs the Config factory. * - * @param Drupal\Core\Config\StorageInterface $storage - * The storage controller object to use for reading and writing - * configuration data. - * @param Symfony\Component\EventDispatcher\EventDispatcher - * An event dispatcher instance to use for configuration events. + * @param Drupal\Core\Config\ContextInterface + * Configuration context object. */ - public function __construct(StorageInterface $storage, EventDispatcher $event_dispatcher) { - $this->storage = $storage; - $this->eventDispatcher = $event_dispatcher; + public function __construct(ContextInterface $context) { + $this->setContext($context); } /** - * Returns a configuration object for a given name. + * Returns a configuration object for a given name and context. * * @param string $name * The name of the configuration object to construct. - * - * @return Drupal\Core\Config\Config - * A configuration object with the given $name. */ public function get($name) { - if (isset($this->cache[$name])) { - return $this->cache[$name]; + $cache_key = get_class($this->getContext()) . ':' . $name; + if (isset($this->cache[$cache_key])) { + return $this->cache[$cache_key]; } - $this->cache[$name] = new Config($name, $this->storage, $this->eventDispatcher); - return $this->cache[$name]->init(); + $this->cache[$cache_key] = new Config($name, $this->getContext()); + return $this->cache[$cache_key]->init(); } /** @@ -86,8 +74,9 @@ public function get($name) { */ public function reset($name = NULL) { if ($name) { - if (isset($this->cache[$name])) { - $this->cache[$name]->init(); + $cache_key = get_class($this->getContext()) . ':' . $name; + if (isset($this->cache[$cache_key])) { + $this->cache[$cache_key]->init(); } } else { @@ -108,14 +97,37 @@ public function reset($name = NULL) { * @todo D8: Remove after http://drupal.org/node/1865206. */ public function rename($old_name, $new_name) { - if (isset($this->cache[$old_name])) { - $config = $this->cache[$old_name]; + $old_cache_key = get_class($this->getContext()) . ':' . $old_name; + $new_cache_key = get_class($this->getContext()) . ':' . $new_name; + if (isset($this->cache[$old_cache_key])) { + $config = $this->cache[$old_cache_key]; // Clone the object into the existing slot. - $this->cache[$old_name] = clone $config; + $this->cache[$old_cache_key] = clone $config; // Change the object's name and re-initialize it. $config->setName($new_name)->init(); - $this->cache[$new_name] = $config; + $this->cache[$new_cache_key] = $config; } } + + /** + * Gets the default context for this factory. + * + * @return Drupal\Core\Config\ContextInterface + * A configuration context instance. + */ + public function getContext() { + return $this->context; + } + + /** + * Sets the default context for this factory. + * + * @param Drupal\Core\Config\ContextInterface $context + * Context to use to get the configuration. + */ + public function setContext($context) { + $this->context = $context; + } } + diff --git a/core/lib/Drupal/Core/Config/Context/AdminContext.php b/core/lib/Drupal/Core/Config/Context/AdminContext.php new file mode 100644 index 0000000..523c9be --- /dev/null +++ b/core/lib/Drupal/Core/Config/Context/AdminContext.php @@ -0,0 +1,40 @@ +set(self::ADMIN, $name); + $this->set($name, TRUE); + parent::__construct($storage ?: drupal_container()->get('config.storage'), drupal_container()->get('event_dispatcher')); + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Config/Context/ConfigContext.php b/core/lib/Drupal/Core/Config/Context/ConfigContext.php new file mode 100644 index 0000000..6d2dcbe --- /dev/null +++ b/core/lib/Drupal/Core/Config/Context/ConfigContext.php @@ -0,0 +1,84 @@ +storage = $storage; + $this->eventDispatcher = $event_dispatcher; + // Notify event listeners that a configuration context has been created. + $this->notify('context', NULL); + } + + /** + * Implements Drupal\Core\Config\ContextInterface::getStorage(). + */ + public function getStorage() { + return $this->storage; + } + + /** + * Implements Drupal\Core\Config\ContextInterface::notify(). + */ + public function notify($config_event_name, Config $config = NULL) { + $this->eventDispatcher->dispatch('config.' . $config_event_name, new ConfigEvent($this, $config)); + } + +} diff --git a/core/lib/Drupal/Core/Config/Context/GlobalContext.php b/core/lib/Drupal/Core/Config/Context/GlobalContext.php new file mode 100644 index 0000000..d7b1915 --- /dev/null +++ b/core/lib/Drupal/Core/Config/Context/GlobalContext.php @@ -0,0 +1,32 @@ +data[self::OVERRIDE] = &$conf; + parent::__construct($storage, $event_dispatcher); + } + +} diff --git a/core/lib/Drupal/Core/Config/Context/ObjectContext.php b/core/lib/Drupal/Core/Config/Context/ObjectContext.php new file mode 100644 index 0000000..bc9480f --- /dev/null +++ b/core/lib/Drupal/Core/Config/Context/ObjectContext.php @@ -0,0 +1,41 @@ +set(self::OBJECT, $name); + $this->set($name, $object); + parent::__construct(drupal_container()->get('config.storage'), drupal_container()->get('event_dispatcher')); + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Config/ContextInterface.php b/core/lib/Drupal/Core/Config/ContextInterface.php new file mode 100644 index 0000000..bec640d --- /dev/null +++ b/core/lib/Drupal/Core/Config/ContextInterface.php @@ -0,0 +1,43 @@ +addArgument(new Reference('config.cachedstorage.storage')) ->addArgument(new Reference('cache.config')); - $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory') + $container->register('config.context', 'Drupal\Core\Config\Context\GlobalContext') ->addArgument(new Reference('config.storage')) - ->addArgument(new Reference('event_dispatcher')) + ->addArgument(new Reference('event_dispatcher')); + $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory') + ->addArgument(new Reference('config.context')) ->addTag('persist'); // Register staging configuration storage. @@ -214,7 +216,7 @@ public function build(ContainerBuilder $container) { ->addTag('event_subscriber'); $container->register('request_close_subscriber', 'Drupal\Core\EventSubscriber\RequestCloseSubscriber') ->addTag('event_subscriber'); - $container->register('config_global_override_subscriber', 'Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber') + $container->register('config_global_override_subscriber', 'Drupal\Core\EventSubscriber\ConfigOverrideSubscriber') ->addTag('event_subscriber'); $container->register('exception_listener', 'Drupal\Core\EventSubscriber\ExceptionListener') ->addTag('event_subscriber') diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php deleted file mode 100644 index 6ee6a3d..0000000 --- a/core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php +++ /dev/null @@ -1,39 +0,0 @@ -getConfig(); - if (isset($conf[$config->getName()])) { - $config->setOverride($conf[$config->getName()]); - } - } - - /** - * Implements EventSubscriberInterface::getSubscribedEvents(). - */ - static function getSubscribedEvents() { - $events['config.init'][] = array('configInit', 30); - return $events; - } -} diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigOverrideSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigOverrideSubscriber.php new file mode 100644 index 0000000..41ca797 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/ConfigOverrideSubscriber.php @@ -0,0 +1,42 @@ +getContext()->get(ConfigContext::OVERRIDE)) { + $config = $event->getConfig(); + if (isset($override[$config->getName()])) { + $config->setOverride($override[$config->getName()]); + } + } + } + + /** + * Implements EventSubscriberInterface::getSubscribedEvents(). + */ + public static function getSubscribedEvents() { + $events['config.init'][] = array('configInit', 30); + return $events; + } +} diff --git a/core/modules/config/config.api.php b/core/modules/config/config.api.php index d845667..28cb90f 100644 --- a/core/modules/config/config.api.php +++ b/core/modules/config/config.api.php @@ -124,3 +124,25 @@ function hook_config_import_delete($name, $new_config, $old_config) { return TRUE; } +/** + * Deletes configuration upon synchronizing configuration changes. + * + * This callback is invoked when a new configuration factory is created and + * allows a module to take over the synchronization of configuration data. + * + * Modules should implement this callback if they manage configuration data + * (such as image styles, node types, or fields) which needs to be prepared and + * passed through module API functions to properly handle a configuration + * change. + * + * @param Drupal\Core\Config\ConfigFactory $factory + * A configuration object containing the new configuration data. + */ +function hook_config_factory($factory) { + if (!$factory->getContext('locale.language')) { + // Add user's language when in user's context. + if ($account = $factory->getContext('user.account')) { + $factory->setContext('locale.language', user_preferred_language($account)); + } + } +} diff --git a/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php b/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php index 3d2fd4a..44df486 100644 --- a/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php +++ b/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php @@ -18,6 +18,23 @@ * $config is always a DrupalConfig object. */ class LocaleConfigSubscriber implements EventSubscriberInterface { + + /** + * Initialize configuration context with language. + * + * @param Drupal\Core\Config\ConfigEvent $event + * The Event to process. + */ + public function configContext(ConfigEvent $event) { + $context = $event->getContext(); + if (!$context->get('locale.language')) { + // Add user's language for user context. + if ($account = $context->get('user.account')) { + $context->set('locale.language', language_load(user_preferred_langcode($account))); + } + } + } + /** * Override configuration values with localized data. * @@ -25,11 +42,13 @@ class LocaleConfigSubscriber implements EventSubscriberInterface { * The Event to process. */ public function configLoad(ConfigEvent $event) { - $config = $event->getConfig(); - $language = language(LANGUAGE_TYPE_INTERFACE); - $locale_name = $this->getLocaleConfigName($config->getName(), $language); - if ($override = $config->getStorage()->read($locale_name)) { - $config->setOverride($override); + $context = $event->getContext(); + if ($language = $context->get('locale.language')) { + $config = $event->getConfig(); + $locale_name = $this->getLocaleConfigName($config->getName(), $language); + if ($override = $context->getStorage()->read($locale_name)) { + $config->setOverride($override); + } } } @@ -47,6 +66,7 @@ public function getLocaleConfigName($name, $language) { * Implements EventSubscriberInterface::getSubscribedEvents(). */ static function getSubscribedEvents() { + $events['config.context'][] = array('configContext', 20); $events['config.load'][] = array('configLoad', 20); return $events; } diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index c8ccc37..8cacd80 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -16,6 +16,7 @@ use Drupal\locale\StringDatabaseStorage; use Drupal\locale\TranslationsStream; use Drupal\Core\Database\Database; +use Drupal\Core\Config\ConfigFactory; /** * Regular expression pattern used to localize JavaScript strings. @@ -1306,6 +1307,8 @@ function _locale_rebuild_js($langcode = NULL) { * Implements hook_language_init(). */ function locale_language_init() { + // Add current language to default configuration context. + drupal_container()->get('config.context')->set('locale.language', language(LANGUAGE_TYPE_INTERFACE)); // Add locale helper to configuration subscribers. drupal_container()->get('event_dispatcher')->addSubscriber(new LocaleConfigSubscriber()); } diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index a5194f7..5aa88bb 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1378,7 +1378,7 @@ function system_modules_uninstall_submit($form, &$form_state) { * @see system_settings_form() */ function system_site_information_settings($form, &$form_state) { - $site_config = config('system.site'); + $site_config = system_config('system.site'); $site_mail = $site_config->get('mail'); if (empty($site_mail)) { $site_mail = ini_get('sendmail_from'); @@ -1484,7 +1484,7 @@ function system_site_information_settings_validate($form, &$form_state) { * Form submission handler for system_site_information_settings(). */ function system_site_information_settings_submit($form, &$form_state) { - config('system.site') + system_config('system.site') ->set('name', $form_state['values']['site_name']) ->set('mail', $form_state['values']['site_mail']) ->set('slogan', $form_state['values']['site_slogan']) @@ -1524,7 +1524,7 @@ function system_cron_settings($form, &$form_state) { $form['cron']['cron_safe_threshold'] = array( '#type' => 'select', '#title' => t('Run cron every'), - '#default_value' => config('system.cron')->get('threshold.autorun'), + '#default_value' => system_config('system.cron')->get('threshold.autorun'), '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval'), ); @@ -1537,7 +1537,7 @@ function system_cron_settings($form, &$form_state) { * @ingroup forms */ function system_cron_settings_submit($form, &$form_state) { - config('system.cron') + system_config('system.cron') ->set('threshold.autorun', $form_state['values']['cron_safe_threshold']) ->save(); } @@ -1569,7 +1569,7 @@ function system_logging_settings($form, &$form_state) { $form['error_level'] = array( '#type' => 'radios', '#title' => t('Error messages to display'), - '#default_value' => config('system.logging')->get('error_level'), + '#default_value' => system_config('system.logging')->get('error_level'), '#options' => array( ERROR_REPORTING_HIDE => t('None'), ERROR_REPORTING_DISPLAY_SOME => t('Errors and warnings'), @@ -1588,7 +1588,7 @@ function system_logging_settings($form, &$form_state) { * @ingroup forms */ function system_logging_settings_submit($form, &$form_state) { - config('system.logging') + system_config('system.logging') ->set('error_level', $form_state['values']['error_level']) ->save(); } @@ -1601,7 +1601,7 @@ function system_logging_settings_submit($form, &$form_state) { */ function system_performance_settings($form, &$form_state) { drupal_add_library('system', 'drupal.system'); - $config = config('system.performance'); + $config = system_config('system.performance'); $form['clear_cache'] = array( '#type' => 'details', @@ -1690,7 +1690,7 @@ function system_performance_settings($form, &$form_state) { * @ingroup forms */ function system_performance_settings_submit($form, &$form_state) { - $config = config('system.performance'); + $config = system_config('system.performance'); $config->set('cache.page.enabled', $form_state['values']['cache']); $config->set('cache.page.max_age', $form_state['values']['page_cache_maximum_age']); $config->set('response.gzip', $form_state['values']['page_compression']); @@ -1815,7 +1815,7 @@ function system_image_toolkit_settings() { * @ingroup forms */ function system_rss_feeds_settings($form, &$form_state) { - $rss_config = config('system.rss'); + $rss_config = system_config('system.rss'); $form['feed_description'] = array( '#type' => 'textarea', '#title' => t('Feed description'), @@ -1850,7 +1850,7 @@ function system_rss_feeds_settings($form, &$form_state) { * @ingroup forms */ function system_rss_feeds_settings_submit($form, &$form_state) { - config('system.rss') + system_config('system.rss') ->set('channel.description', $form_state['values']['feed_description']) ->set('items.limit', $form_state['values']['feed_default_items']) ->set('items.view_mode', $form_state['values']['feed_item_length']) @@ -1970,7 +1970,7 @@ function system_regional_settings_submit($form, &$form_state) { * @see system_site_maintenance_mode_submit() */ function system_site_maintenance_mode($form, &$form_state) { - $config = config('system.maintenance'); + $config = system_config('system.maintenance'); $form['maintenance_mode'] = array( '#type' => 'checkbox', '#title' => t('Put site into maintenance mode'), @@ -1992,7 +1992,7 @@ function system_site_maintenance_mode($form, &$form_state) { * @ingroup forms */ function system_site_maintenance_mode_submit($form, &$form_state) { - config('system.maintenance') + system_config('system.maintenance') ->set('enabled', $form_state['values']['maintenance_mode']) ->set('message', $form_state['values']['maintenance_mode_message']) ->save(); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 709fd1f..fb686fe 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -7,6 +7,7 @@ use Drupal\Core\Utility\ModuleInfo; use Drupal\Core\TypedData\Primitive; +use Drupal\Core\Config\Context\AdminContext; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -3393,6 +3394,37 @@ function system_admin_compact_page($mode = 'off') { } /** + * Retrieves a configuration object for administration. + * + * This configuration object will be created using a special configuration + * context (config.admin = TRUE) and configuration plug-ins should mostly keep + * hands off it and don't override it. + * + * This is intended for administration forms or any other case when we need to + * get / set the configuration data without any overrides, as oppossed to + * regular configuration used for runtime operations that may be altered by + * plug-ins depending on other page request parameters: + * + * To get regular configuration objects for the page request use config() + * instead of this function. + * + * @param $name + * The name of the configuration object to retrieve. + * + * @return Drupal\Core\Config\Config + * A configuration object. + * + * @see config() + */ +function system_config($name) { + static $context; + if (!isset($context)) { + $context = new AdminContext('system.config', drupal_container()->get('config.storage')); + } + return config($name, $context); +} + +/** * Generate a list of tasks offered by a specified module. * * @param $module diff --git a/core/modules/user/lib/Drupal/user/UserConfigContext.php b/core/modules/user/lib/Drupal/user/UserConfigContext.php new file mode 100644 index 0000000..ec2a89d --- /dev/null +++ b/core/modules/user/lib/Drupal/user/UserConfigContext.php @@ -0,0 +1,28 @@ + array( + 'title' => t('User account'), + 'class' => '\Drupal\user\UserConfigContext', + ), + ); +} + +/** * Implements hook_template_preprocess_default_variables_alter(). * * @see user_user_login() @@ -1915,35 +1928,27 @@ function user_view_multiple($accounts, $view_mode = 'full', $langcode = NULL) { function user_mail($key, &$message, $params) { $langcode = $message['langcode']; $variables = array('user' => $params['account']); - $message['subject'] .= _user_mail_text($key . '.subject', $langcode, $variables); - $message['body'][] = _user_mail_text($key . '.body', $langcode, $variables); -} -/** - * Returns a mail string for a variable name. - * - * @param string $key - * The config key that provides the mail text. - * @param string $langcode - * (optional) A language code to use to generate the e-mail text. - * @param array $variables - * (optional) An array of token keys and values. - * - * @return - * A string value containing the text for the user.mail config key. - */ -function _user_mail_text($key, $langcode = NULL, $variables = array()) { + // Get configuration objects customized for this user, that may be localized + // for the user's language if the locale module is enabled. + $config_context = config_factory_set_context(new UserConfigContext($params['account'])); + $mail_config = config('user.mail'); + // We do not sanitize the token replacement, since the output of this // replacement is intended for an e-mail message, not a web browser. - return token_replace(config('user.mail')->get($key), $variables, array('langcode' => $langcode, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE)); + $token_options = array('langcode' => $langcode, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE); + $message['subject'] .= token_replace($mail_config->get($key . '.subject'), $variables, $token_options); + $message['body'][] = token_replace($mail_config->get($key . '.body'), $variables, $token_options); + + // Set the configuration context back to the original. + config_factory_set_context($config_context); } /** * Token callback to add unsafe tokens for user mails. * - * This function is used by the token_replace() call at the end of - * _user_mail_text() to set up some additional tokens that can be - * used in email messages generated by user_mail(). + * This function is used by the token_replace() to set up some additional + * tokens that can be used in email messages generated by user_mail(). * * @param $replacements * An associative array variable containing mappings from token names to