diff -u b/core/includes/config.inc b/core/includes/config.inc --- b/core/includes/config.inc +++ b/core/includes/config.inc @@ -1,6 +1,7 @@ get('config.factory')->get($name); } +/* + * Sets the config context on the config factory. + * + * This allows configuration objects to be created using special configuration + * contexts eg. global override free or locale using a user preferred language. + * Calling this function affects all subsequent calls to config() until + * drupal_container()->get('config.context.factory')->leaveContext() is called. + * + * @param string $context_name + * The name of the config context service on the container or a fully + * qualified class implementing \Drupal\Core\Config\Context\ContextInterface. + * + * @return \Drupal\Core\Config\Context\ContextInterface + * The configuration context object. + */ +function config_context_enter($context_name) { + if (drupal_container()->has($context_name)) { + $context = drupal_container()->get($context_name); + } + elseif (class_exists($context_name)) { + $context = drupal_container() + ->get('config.context.factory') + ->get($context_name); + } + else { + throw new ConfigException(sprintf('Unknown config context service or class: %s', $context_name)); + } + drupal_container() + ->get('config.factory') + ->enterContext($context); + return $context; +} + /** * Returns a list of differences between configuration storages. * diff -u b/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php b/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php --- b/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php +++ b/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Config\Context; use Drupal\Core\Config\Config; +use Drupal\Core\Config\ConfigException; use Drupal\Core\Config\StorageInterface; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -56,7 +57,7 @@ $context = new $class($this->eventDispatcher); } else { - throw new \Drupal\Core\Config\ConfigException(sprintf('Unknown config context class: %s', $class)); + throw new ConfigException(sprintf('Unknown config context class: %s', $class)); } return $context; } diff -u b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php --- b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php @@ -7,6 +7,7 @@ namespace Drupal\config\Tests; +use Drupal\Core\Config\ConfigException; use Drupal\Core\Language\Language; use Drupal\simpletest\DrupalUnitTestBase; @@ -45,13 +46,10 @@ $this->assertIdentical($config->get('foo'), 'en bar'); // Ensure that we get the expected value when we use system_config. - $config_admin = system_config('config_test.system'); + config_context_enter('config.context.free'); + $config_admin = config('config_test.system'); $this->assertIdentical($config_admin->get('foo'), 'bar'); - // We should still be in the non override context. - $config = config($name); - $this->assertIdentical($config->get('foo'), 'bar'); - // Leave the non override context. drupal_container()->get('config.factory')->leaveContext(); $config = config($name); @@ -85,11 +83,10 @@ 'status' => 1, 'preferred_langcode' => 'fr', )); - - $user_config_context = drupal_container()->get('config.context.factory')->get('Drupal\user\UserConfigContext'); $config_factory = drupal_container()->get('config.factory'); - $config_factory->enterContext($user_config_context->setAccount($account)); + $user_config_context = config_context_enter('Drupal\user\UserConfigContext'); + $user_config_context->setAccount($account); $config = config('config_test.system'); $this->assertIdentical($config->get('foo'), 'fr bar'); @@ -121,8 +118,8 @@ 'preferred_langcode' => 'en', )); // Create a new user config context to stack on top of the existign one. - $en_user_config_context = drupal_container()->get('config.context.factory')->get('Drupal\user\UserConfigContext'); - $config_factory->enterContext($en_user_config_context->setAccount($account)); + $en_user_config_context = config_context_enter('Drupal\user\UserConfigContext'); + $en_user_config_context->setAccount($account); $config = config('config_test.system'); $this->assertIdentical($config->get('foo'), 'en bar'); @@ -146,2 +143,15 @@ + /* + * Tests config_context_enter() invalid context name handling. + */ + function testInvalidContextName() { + $message = 'Expected ConfigException was thrown for an invalid context_name argument.'; + try { + config_context_enter('invalid.config.context'); + $this->fail($message); + } + catch (ConfigException $e) { + $this->pass($message); + } + } } diff -u b/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc --- b/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1403,7 +1403,8 @@ * @see system_settings_form() */ function system_site_information_settings($form, &$form_state) { - $site_config = system_config('system.site'); + config_context_enter('config.context.free'); + $site_config = config('system.site'); $site_mail = $site_config->get('mail'); if (empty($site_mail)) { $site_mail = ini_get('sendmail_from'); @@ -1509,7 +1510,8 @@ * Form submission handler for system_site_information_settings(). */ function system_site_information_settings_submit($form, &$form_state) { - system_config('system.site') + config_context_enter('config.context.free'); + config('system.site') ->set('name', $form_state['values']['site_name']) ->set('mail', $form_state['values']['site_mail']) ->set('slogan', $form_state['values']['site_slogan']) @@ -1525,6 +1527,7 @@ * @ingroup forms */ function system_cron_settings($form, &$form_state) { + config_context_enter('config.context.free'); $form['description'] = array( '#markup' => '
' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '
', ); @@ -1549,7 +1552,7 @@ $form['cron']['cron_safe_threshold'] = array( '#type' => 'select', '#title' => t('Run cron every'), - '#default_value' => system_config('system.cron')->get('threshold.autorun'), + '#default_value' => config('system.cron')->get('threshold.autorun'), '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval'), ); @@ -1562,7 +1565,8 @@ * @ingroup forms */ function system_cron_settings_submit($form, &$form_state) { - system_config('system.cron') + config_context_enter('config.context.free'); + config('system.cron') ->set('threshold.autorun', $form_state['values']['cron_safe_threshold']) ->save(); } @@ -1591,10 +1595,11 @@ * @see system_logging_settings_submit() */ function system_logging_settings($form, &$form_state) { + config_context_enter('config.context.free'); $form['error_level'] = array( '#type' => 'radios', '#title' => t('Error messages to display'), - '#default_value' => system_config('system.logging')->get('error_level'), + '#default_value' => config('system.logging')->get('error_level'), '#options' => array( ERROR_REPORTING_HIDE => t('None'), ERROR_REPORTING_DISPLAY_SOME => t('Errors and warnings'), @@ -1613,7 +1618,8 @@ * @ingroup forms */ function system_logging_settings_submit($form, &$form_state) { - system_config('system.logging') + config_context_enter('config.context.free'); + config('system.logging') ->set('error_level', $form_state['values']['error_level']) ->save(); } @@ -1626,7 +1632,8 @@ */ function system_performance_settings($form, &$form_state) { drupal_add_library('system', 'drupal.system'); - $config = system_config('system.performance'); + config_context_enter('config.context.free'); + $config = config('system.performance'); $form['clear_cache'] = array( '#type' => 'details', @@ -1715,7 +1722,8 @@ * @ingroup forms */ function system_performance_settings_submit($form, &$form_state) { - $config = system_config('system.performance'); + config_context_enter('config.context.free'); + $config = 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']); @@ -1860,7 +1868,8 @@ * @ingroup forms */ function system_rss_feeds_settings($form, &$form_state) { - $rss_config = system_config('system.rss'); + config_context_enter('config.context.free'); + $rss_config = config('system.rss'); $form['feed_description'] = array( '#type' => 'textarea', '#title' => t('Feed description'), @@ -1895,7 +1904,8 @@ * @ingroup forms */ function system_rss_feeds_settings_submit($form, &$form_state) { - system_config('system.rss') + config_context_enter('config.context.free'); + 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']) @@ -2015,7 +2025,8 @@ * @see system_site_maintenance_mode_submit() */ function system_site_maintenance_mode($form, &$form_state) { - $config = system_config('system.maintenance'); + config_context_enter('config.context.free'); + $config = config('system.maintenance'); $form['maintenance_mode'] = array( '#type' => 'checkbox', '#title' => t('Put site into maintenance mode'), @@ -2037,7 +2048,8 @@ * @ingroup forms */ function system_site_maintenance_mode_submit($form, &$form_state) { - system_config('system.maintenance') + config_context_enter('config.context.free'); + config('system.maintenance') ->set('enabled', $form_state['values']['maintenance_mode']) ->set('message', $form_state['values']['maintenance_mode_message']) ->save(); reverted: --- b/core/modules/system/system.module +++ a/core/modules/system/system.module @@ -3442,35 +3442,6 @@ } /** - * 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) { - $config_factory = drupal_container()->get('config.factory'); - $config_factory->enterContext(drupal_container()->get('config.context.free')); - return config($name); -} - -/** * Generate a list of tasks offered by a specified module. * * @param $module diff -u b/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc --- b/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -292,8 +292,9 @@ * @see user_admin_settings_submit() */ function user_admin_settings($form, &$form_state) { - $config = system_config('user.settings'); - $mail_config = system_config('user.mail'); + config_context_enter('config.context.free'); + $config = config('user.settings'); + $mail_config = config('user.mail'); // Settings for anonymous users. $form['anonymous_settings'] = array( @@ -627,6 +628,7 @@ * Form submission handler for user_admin_settings(). */ function user_admin_settings_submit($form, &$form_state) { + config_context_enter('config.context.free'); config('user.settings') ->set('anonymous', $form_state['values']['anonymous']) ->set('admin_role', $form_state['values']['user_admin_role']) diff -u b/core/modules/user/user.module b/core/modules/user/user.module --- b/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -8,7 +8,6 @@ use Drupal\user\Plugin\Core\Entity\User; use Drupal\user\UserRole; use Drupal\Core\Template\Attribute; -use Drupal\user\UserConfigContext; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -1759,9 +1758,8 @@ // Get configuration objects customized for this user, that may be localized // for the user's language if the locale module is enabled. - $config_factory = drupal_container()->get('config.factory'); - $user_config_context = drupal_container()->get('config.context.factory')->get('Drupal\user\UserConfigContext'); - $config_factory->enterContext($user_config_context->setAccount($params['account'])); + $user_config_context = config_context_enter("Drupal\\user\\UserConfigContext"); + $user_config_context->setAccount($params['account']); $mail_config = config('user.mail'); // We do not sanitize the token replacement, since the output of this @@ -1769,7 +1767,7 @@ $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); - $config_factory->leaveContext(); + drupal_container()->get('config.factory')->leaveContext(); } /**