From 53a66504f318e86ffde26236913f407cc2e43031 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Mon, 27 Jan 2014 12:22:53 +0000
Subject: [PATCH] =?UTF-8?q?Revert=20"Issue=20#2172561=20by=20G=C3=A1bor=20?=
 =?UTF-8?q?Hojtsy:=20Config=20overrides=20may=20spill=20over=20to=20undesi?=
 =?UTF-8?q?red=20places."?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This reverts commit f2769f4db32f69a68ad4391d60d3847b9da538e6.
---
 core/core.services.yml                        |  4 ++
 core/includes/menu.inc                        |  6 +--
 core/lib/Drupal/Core/Config/ConfigFactory.php | 20 ++++-----
 .../Drupal/Core/Config/ConfigInstaller.php    |  5 +--
 .../ConfigGlobalOverrideSubscriber.php        | 42 +++++++++++++++++++
 core/lib/Drupal/Core/Form/ConfigFormBase.php  |  5 +--
 .../config/Tests/ConfigLanguageOverride.php   |  5 +--
 .../Tests/ConfigModuleOverridesTest.php       | 16 +++----
 .../config/Tests/ConfigOverrideTest.php       |  5 +--
 .../Tests/ConfigOverridesPriorityTest.php     |  4 +-
 .../Form/ConfigTranslationFormBase.php        | 17 ++++----
 .../LocaleAdminPathConfigEntityConverter.php  |  5 +--
 .../Tests/d6/MigrateSystemConfigsTest.php     |  5 +--
 .../search/SearchPageListController.php       |  5 +--
 14 files changed, 85 insertions(+), 59 deletions(-)
 create mode 100644 core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php

diff --git a/core/core.services.yml b/core/core.services.yml
index f00c4502e3f4..5c38b4b2ebcc 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -519,6 +519,10 @@ services:
     tags:
       - { name: event_subscriber }
     arguments: ['@module_handler']
+  config_global_override_subscriber:
+    class: Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber
+    tags:
+      - { name: event_subscriber }
   config_import_subscriber:
     class: Drupal\Core\EventSubscriber\ConfigImportSubscriber
     tags:
diff --git a/core/includes/menu.inc b/core/includes/menu.inc
index d54fe068cf1a..c3a2162dfbc6 100644
--- a/core/includes/menu.inc
+++ b/core/includes/menu.inc
@@ -2556,8 +2556,7 @@ function menu_router_rebuild() {
 function menu_router_build($save = FALSE) {
   // Ensure that all configuration used to build the menu items are loaded
   // without overrides.
-  $old_state = \Drupal::configFactory()->getOverrideState();
-  \Drupal::configFactory()->setOverrideState(FALSE);
+  \Drupal::configFactory()->disableOverrides();
   // We need to manually call each module so that we can know which module
   // a given item came from.
   $callbacks = array();
@@ -2572,7 +2571,8 @@ function menu_router_build($save = FALSE) {
   }
   // Alter the menu as defined in modules, keys are like user/%user.
   drupal_alter('menu', $callbacks);
-  \Drupal::configFactory()->setOverrideState($old_state);
+  // Re-enable configuration overrides.
+  \Drupal::configFactory()->enableOverrides();
   foreach ($callbacks as $path => $router_item) {
     // If the menu item is a default local task and incorrectly references a
     // route, remove it.
diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php
index c8d3f3acbb53..e277787e6816 100644
--- a/core/lib/Drupal/Core/Config/ConfigFactory.php
+++ b/core/lib/Drupal/Core/Config/ConfigFactory.php
@@ -91,27 +91,25 @@ public function __construct(StorageInterface $storage, EventDispatcher $event_di
   }
 
   /**
-   * Set the override state.
-   *
-   * @param bool $state
-   *   TRUE if overrides should be applied, FALSE otherwise.
+   * Disable overrides when loading configuration objects.
    *
    * @return \Drupal\Core\Config\ConfigFactory
    *   The config factory object.
    */
-  public function setOverrideState($state) {
-    $this->useOverrides = $state;
+  public function disableOverrides() {
+    $this->useOverrides = FALSE;
     return $this;
   }
 
   /**
-   * Get the override state.
+   * Enable overrides when loading configuration objects.
    *
-   * @return bool
-   *   TRUE if overrides are applied, FALSE otherwise.
+   * @return \Drupal\Core\Config\ConfigFactory
+   *   The config factory object.
    */
-  public function getOverrideState() {
-    return $this->useOverrides;
+  public function enableOverrides() {
+    $this->useOverrides = TRUE;
+    return $this;
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php
index 20d45fa4a2ed..dc4f3b928cd8 100644
--- a/core/lib/Drupal/Core/Config/ConfigInstaller.php
+++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php
@@ -101,8 +101,7 @@ public function installDefaultConfig($type, $name) {
     }
 
     if (!empty($config_to_install)) {
-      $old_state = $this->configFactory->getOverrideState();
-      $this->configFactory->setOverrideState(FALSE);
+      $this->configFactory->disableOverrides();
       foreach ($config_to_install as $name) {
         // Only import new config.
         if ($this->activeStorage->exists($name)) {
@@ -124,7 +123,7 @@ public function installDefaultConfig($type, $name) {
           $new_config->save();
         }
       }
-      $this->configFactory->setOverrideState($old_state);
+      $this->configFactory->enableOverrides();
     }
   }
 
diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php
new file mode 100644
index 000000000000..02bf2cbfad78
--- /dev/null
+++ b/core/lib/Drupal/Core/EventSubscriber/ConfigGlobalOverrideSubscriber.php
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber.
+ */
+
+namespace Drupal\Core\EventSubscriber;
+
+use Drupal\Core\Config\Config;
+use Drupal\Core\Config\ConfigEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * Defines a configuration global override for contexts.
+ */
+class ConfigGlobalOverrideSubscriber implements EventSubscriberInterface {
+
+  /**
+   * Overrides configuration values with values in global $conf variable.
+   *
+   * @param \Drupal\Core\Config\ConfigEvent $event
+   *   The Event to process.
+   */
+  public function configInit(ConfigEvent $event) {
+    global $conf;
+
+    $config = $event->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/Form/ConfigFormBase.php b/core/lib/Drupal/Core/Form/ConfigFormBase.php
index 5a4abb22a07f..91a91d5d54ba 100644
--- a/core/lib/Drupal/Core/Form/ConfigFormBase.php
+++ b/core/lib/Drupal/Core/Form/ConfigFormBase.php
@@ -74,10 +74,9 @@ public function submitForm(array &$form, array &$form_state) {
    * configuration.
    */
   protected function config($name) {
-    $old_state = $this->configFactory->getOverrideState();
-    $this->configFactory->setOverrideState(FALSE);
+    $this->configFactory->disableOverrides();
     $config = $this->configFactory->get($name);
-    $this->configFactory->setOverrideState($old_state);
+    $this->configFactory->enableOverrides();
     return $config;
   }
 }
diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigLanguageOverride.php b/core/modules/config/lib/Drupal/config/Tests/ConfigLanguageOverride.php
index 12ff3cd2d9ee..2f939507ae8f 100644
--- a/core/modules/config/lib/Drupal/config/Tests/ConfigLanguageOverride.php
+++ b/core/modules/config/lib/Drupal/config/Tests/ConfigLanguageOverride.php
@@ -82,11 +82,10 @@ function testConfigLanguageOverride() {
     $config = \Drupal::config('config_test.new');
     $this->assertTrue($config->isNew(), 'The configuration object config_test.new is new');
     $this->assertIdentical($config->get('language'), 'override');
-    $old_state = \Drupal::configFactory()->getOverrideState();
-    \Drupal::configFactory()->setOverrideState(FALSE);
+    \Drupal::configFactory()->disableOverrides();
     $config = \Drupal::config('config_test.new');
     $this->assertIdentical($config->get('language'), NULL);
-    \Drupal::configFactory()->setOverrideState($old_state);
+    \Drupal::configFactory()->enableOverrides();
 
     // Ensure that language configuration overrides can not be overridden.
     global $conf;
diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigModuleOverridesTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigModuleOverridesTest.php
index c5c7190c3788..0d4569374b83 100644
--- a/core/modules/config/lib/Drupal/config/Tests/ConfigModuleOverridesTest.php
+++ b/core/modules/config/lib/Drupal/config/Tests/ConfigModuleOverridesTest.php
@@ -38,31 +38,25 @@ public function testSimpleModuleOverrides() {
       ->set('slogan', $non_overridden_slogan)
       ->save();
 
-    $this->assertTrue($config_factory->getOverrideState(), 'By default ConfigFactory has overrides enabled.');
-
-    $old_state = $config_factory->getOverrideState();
-
-    $config_factory->setOverrideState(FALSE);
-    $this->assertFalse($config_factory->getOverrideState(), 'ConfigFactory can disable overrides.');
+    $config_factory->disableOverrides();
     $this->assertEqual($non_overridden_name, $config_factory->get('system.site')->get('name'));
     $this->assertEqual($non_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
 
-    $config_factory->setOverrideState(TRUE);
-    $this->assertTrue($config_factory->getOverrideState(), 'ConfigFactory can enable overrides.');
+    $config_factory->enableOverrides();
     $this->assertEqual($overridden_name, $config_factory->get('system.site')->get('name'));
     $this->assertEqual($overridden_slogan, $config_factory->get('system.site')->get('slogan'));
 
     // Test overrides of completely new configuration objects. In normal runtime
     // this should only happen for configuration entities as we should not be
     // creating simple configuration objects on the fly.
-    $config = $config_factory->get('config_override.new');
+    $config = \Drupal::config('config_override.new');
     $this->assertTrue($config->isNew(), 'The configuration object config_override.new is new');
     $this->assertIdentical($config->get('module'), 'override');
-    $config_factory->setOverrideState(FALSE);
+    \Drupal::configFactory()->disableOverrides();
     $config = \Drupal::config('config_override.new');
     $this->assertIdentical($config->get('module'), NULL);
+    \Drupal::configFactory()->enableOverrides();
 
-    $config_factory->setOverrideState($old_state);
     unset($GLOBALS['config_test_run_module_overrides']);
   }
 }
diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php
index a53565145352..f6f3f46c79e3 100644
--- a/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php
+++ b/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php
@@ -122,15 +122,14 @@ function testConfOverride() {
     $config = \Drupal::config('config_test.new');
     $this->assertTrue($config->isNew(), 'The configuration object config_test.new is new');
     $this->assertIdentical($config->get('key'), 'override');
-    $old_state = \Drupal::configFactory()->getOverrideState();
-    \Drupal::configFactory()->setOverrideState(FALSE);
+    \Drupal::configFactory()->disableOverrides();
     $config_raw = \Drupal::config('config_test.new');
     $this->assertIdentical($config_raw->get('key'), NULL);
     $config_raw
       ->set('key', 'raw')
       ->set('new_key', 'new_value')
       ->save();
-    \Drupal::configFactory()->setOverrideState($old_state);
+    \Drupal::configFactory()->enableOverrides();
     // Ensure override is preserved but all other data has been updated
     // accordingly.
     $this->assertIdentical($config->get('key'), 'override');
diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigOverridesPriorityTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigOverridesPriorityTest.php
index b0567311252d..76573765d32c 100644
--- a/core/modules/config/lib/Drupal/config/Tests/ConfigOverridesPriorityTest.php
+++ b/core/modules/config/lib/Drupal/config/Tests/ConfigOverridesPriorityTest.php
@@ -92,13 +92,11 @@ public function testOverridePriorities() {
     $this->assertEqual($language_overridden_mail, $config_factory->get('system.site')->get('mail'));
     $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));
 
-    $old_state = $config_factory->getOverrideState();
-    $config_factory->setOverrideState(FALSE);
+    $config_factory->disableOverrides();
     $this->assertEqual($non_overridden_name, $config_factory->get('system.site')->get('name'));
     $this->assertEqual($non_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
     $this->assertEqual($non_overridden_mail, $config_factory->get('system.site')->get('mail'));
     $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));
-    $config_factory->setOverrideState($old_state);
 
     unset($GLOBALS['config_test_run_module_overrides']);
   }
diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php b/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php
index 9370e59f3cc2..9295b81af4a4 100644
--- a/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php
+++ b/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php
@@ -163,13 +163,12 @@ public function buildForm(array $form, array &$form_state, Request $request = NU
     $this->language = $language;
     $this->sourceLanguage = $this->mapper->getLanguageWithFallback();
 
-    // Get base language configuration to display in the form before setting the
-    // language to use for the form. This avoids repetitively setting and
-    // resetting the language to get original values later.
-    $old_state = $this->configFactory->getOverrideState();
-    $this->configFactory->setOverrideState(FALSE);
+    // Get base language configuration to display in the form before entering
+    // into the language context for the form. This avoids repetitively going
+    // in and out of the language context to get original values later.
+    $this->configFactory->disableOverrides();
     $this->baseConfigData = $this->mapper->getConfigData();
-    $this->configFactory->setOverrideState($old_state);
+    $this->configFactory->enableOverrides();
 
     // Set the translation target language on the configuration factory.
     $original_language = $this->configFactory->getLanguage();
@@ -211,9 +210,7 @@ public function submitForm(array &$form, array &$form_state) {
     $form_values = $form_state['values']['config_names'];
 
     // For the form submission handling, use the raw data.
-    $old_state = $this->configFactory->getOverrideState();
-    $this->configFactory->setOverrideState(FALSE);
-
+    $this->configFactory->disableOverrides();
     foreach ($this->mapper->getConfigNames() as $name) {
       // Set configuration values based on form submission and source values.
       $base_config = $this->config($name);
@@ -232,7 +229,7 @@ public function submitForm(array &$form, array &$form_state) {
         $translation_config->save();
       }
     }
-    $this->configFactory->setOverrideState($old_state);
+    $this->configFactory->enableOverrides();
 
     $form_state['redirect_route'] = array(
       'route_name' => $this->mapper->getOverviewRoute(),
diff --git a/core/modules/locale/lib/Drupal/locale/ParamConverter/LocaleAdminPathConfigEntityConverter.php b/core/modules/locale/lib/Drupal/locale/ParamConverter/LocaleAdminPathConfigEntityConverter.php
index 2eca5dda759f..6488eb4733b2 100644
--- a/core/modules/locale/lib/Drupal/locale/ParamConverter/LocaleAdminPathConfigEntityConverter.php
+++ b/core/modules/locale/lib/Drupal/locale/ParamConverter/LocaleAdminPathConfigEntityConverter.php
@@ -55,10 +55,9 @@ public function convert($value, $definition, $name, array $defaults, Request $re
     $entity_type = substr($definition['type'], strlen('entity:'));
     if ($storage = $this->entityManager->getStorageController($entity_type)) {
       // Make sure no overrides are loaded.
-      $old_state = $this->configFactory->getOverrideState();
-      $this->configFactory->setOverrideState(FALSE);
+      $this->configFactory->disableOverrides();
       $entity = $storage->load($value);
-      $this->configFactory->setOverrideState($old_state);
+      $this->configFactory->enableOverrides();
       return $entity;
     }
   }
diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateSystemConfigsTest.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateSystemConfigsTest.php
index 0764d72adb04..36b0fa628d8c 100644
--- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateSystemConfigsTest.php
+++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateSystemConfigsTest.php
@@ -181,12 +181,11 @@ public function testSystemFile() {
     $this->prepare($migration, $dumps);
     $executable = new MigrateExecutable($migration, new MigrateMessage());
     $executable->import();
-    $old_state = \Drupal::configFactory()->getOverrideState();
-    \Drupal::configFactory()->setOverrideState(FALSE);
+    \Drupal::configFactory()->disableOverrides();
     $config = \Drupal::config('system.file');
     $this->assertIdentical($config->get('path.private'), 'files/test');
     $this->assertIdentical($config->get('path.temporary'), 'files/temp');
-    \Drupal::configFactory()->setOverrideState($old_state);
+    \Drupal::configFactory()->enableOverrides();
   }
 
 }
diff --git a/core/modules/search/lib/Drupal/search/SearchPageListController.php b/core/modules/search/lib/Drupal/search/SearchPageListController.php
index 4586a6b954e0..e7a7d8b2bd4a 100644
--- a/core/modules/search/lib/Drupal/search/SearchPageListController.php
+++ b/core/modules/search/lib/Drupal/search/SearchPageListController.php
@@ -138,9 +138,8 @@ public function buildRow(EntityInterface $entity) {
    */
   public function buildForm(array $form, array &$form_state) {
     $form = parent::buildForm($form, $form_state);
-    $old_state = $this->configFactory->getOverrideState();
-    $search_settings = $this->configFactory->setOverrideState(FALSE)->get('search.settings');
-    $this->configFactory->setOverrideState($old_state);
+    $search_settings = $this->configFactory->disableOverrides()->get('search.settings');
+    $this->configFactory->enableOverrides();
     // Collect some stats.
     $remaining = 0;
     $total = 0;
-- 
GitLab