diff --git a/core/lib/Drupal/Core/Config/ImmutableConfig.php b/core/lib/Drupal/Core/Config/ImmutableConfig.php index 828f8ba..90302cb 100644 --- a/core/lib/Drupal/Core/Config/ImmutableConfig.php +++ b/core/lib/Drupal/Core/Config/ImmutableConfig.php @@ -1,13 +1,13 @@ $this->getName(), '@key' => $key))); } /** * {@inheritdoc} */ public function clear($key) { - throw new ImmutableConfigException(); + throw new ImmutableConfigException(String::format('@config_name:@key cannot be cleared on an immutable object', array('@config_name' => $this->getName(), '@key' => $key))); } /** * {@inheritdoc} */ public function save() { - throw new ImmutableConfigException(); + throw new ImmutableConfigException(String::format('@config_name cannot be saved, it is an immutable object', array('@config_name' => $this->getName()))); } /** - * Deletes the configuration object. - * - * @return \Drupal\Core\Config\Config - * The configuration object. + * {@inheritdoc} */ public function delete() { - throw new ImmutableConfigException(); + throw new ImmutableConfigException(String::format('@config_name cannot be deleted, it is an immutable object', array('@config_name' => $this->getName()))); } } diff --git a/core/lib/Drupal/Core/Config/ImmutableConfigException.php b/core/lib/Drupal/Core/Config/ImmutableConfigException.php index f5e0e17..fca05a3 100644 --- a/core/lib/Drupal/Core/Config/ImmutableConfigException.php +++ b/core/lib/Drupal/Core/Config/ImmutableConfigException.php @@ -1,9 +1,8 @@ getConfig()->getOriginal('definitions', FALSE); - // Combine with any existing data. - $all_overrides[$id] = $definition + $this->loadOverride($id); - $this->getConfig()->set('definitions', $all_overrides)->save(); + $existing_override = $this->getConfig()->getOriginal($config_key, FALSE); + if ($existing_override) { + // Combine with any existing data. + $definition += $existing_override; + } + $this->getConfig()->set($config_key, $definition)->save(); } return array_keys($definition); } diff --git a/core/modules/config/src/Form/ConfigSingleImportForm.php b/core/modules/config/src/Form/ConfigSingleImportForm.php index 58156b0..d67fc12 100644 --- a/core/modules/config/src/Form/ConfigSingleImportForm.php +++ b/core/modules/config/src/Form/ConfigSingleImportForm.php @@ -265,4 +265,20 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } } + /** + * {@inheritdoc} + * + * Overrides \Drupal\Core\Form\FormBase::config() so that configuration is + * returned override free. This ensures that overrides do not pollute saved + * configuration. + */ + protected function config($name) { + $config_factory = $this->configFactory(); + $old_state = $config_factory->getOverrideState(); + $config_factory->setOverrideState(FALSE); + $config = $config_factory->get($name); + $config_factory->setOverrideState($old_state); + return $config; + } + } diff --git a/core/modules/contact/src/ContactFormEditForm.php b/core/modules/contact/src/ContactFormEditForm.php index 123ba38..ffda57f 100644 --- a/core/modules/contact/src/ContactFormEditForm.php +++ b/core/modules/contact/src/ContactFormEditForm.php @@ -7,9 +7,11 @@ namespace Drupal\contact; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base form for contact form edit forms. @@ -17,13 +19,39 @@ class ContactFormEditForm extends EntityForm { /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + */ + public function __construct(ConfigFactoryInterface $config_factory) { + $this->configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory') + ); + } + + /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $contact_form = $this->entity; - $default_form = $this->config('contact.settings')->get('default_form'); + // Get value without any overrides so we don't copy overrides into active + // configuration. + $default_form = $this->configFactory->get('contact.settings')->getOriginal('default_form', FALSE); $form['label'] = array( '#type' => 'textfield', @@ -94,7 +122,7 @@ public function validate(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) { $contact_form = $this->entity; $status = $contact_form->save(); - $contact_settings = $this->config('contact.settings'); + $contact_settings = $this->configFactory->get('contact.settings'); $edit_link = $this->entity->link($this->t('Edit')); if ($status == SAVED_UPDATED) { diff --git a/core/modules/system/src/Form/ThemeAdminForm.php b/core/modules/system/src/Form/ThemeAdminForm.php index 2cee8c5..93cace8 100644 --- a/core/modules/system/src/Form/ThemeAdminForm.php +++ b/core/modules/system/src/Form/ThemeAdminForm.php @@ -6,13 +6,13 @@ namespace Drupal\system\Form; -use Drupal\Core\Form\FormBase; +use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; /** * Form to select the administration theme. */ -class ThemeAdminForm extends FormBase { +class ThemeAdminForm extends ConfigFormBase { /** * {@inheritdoc} @@ -50,7 +50,7 @@ public function buildForm(array $form, FormStateInterface $form_state, array $th * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - drupal_set_message($this->t('The configuration options have been saved.')); + parent::submitForm($form, $form_state); $this->config('system.theme')->set('admin', $form_state->getValue('admin_theme'))->save(); } diff --git a/core/modules/system/tests/modules/form_test/src/FormTestArgumentsObject.php b/core/modules/system/tests/modules/form_test/src/FormTestArgumentsObject.php index da7b4d5..3f56422 100644 --- a/core/modules/system/tests/modules/form_test/src/FormTestArgumentsObject.php +++ b/core/modules/system/tests/modules/form_test/src/FormTestArgumentsObject.php @@ -55,7 +55,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { drupal_set_message($this->t('The FormTestArgumentsObject::submitForm() method was used for this form.')); - $this->config('form_test.object') + $this->configFactory()->get('form_test.object') ->set('bananas', $form_state->getValue('bananas')) ->save(); } diff --git a/core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php b/core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php index a3e5064..ae58495 100644 --- a/core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php +++ b/core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php @@ -65,7 +65,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { drupal_set_message($this->t('The FormTestControllerObject::submitForm() method was used for this form.')); - $this->config('form_test.object') + $this->configFactory()->get('form_test.object') ->set('bananas', $form_state->getValue('bananas')) ->save(); } diff --git a/core/modules/system/tests/modules/form_test/src/FormTestObject.php b/core/modules/system/tests/modules/form_test/src/FormTestObject.php index 4669bfe..0274a91 100644 --- a/core/modules/system/tests/modules/form_test/src/FormTestObject.php +++ b/core/modules/system/tests/modules/form_test/src/FormTestObject.php @@ -56,7 +56,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { drupal_set_message($this->t('The FormTestObject::submitForm() method was used for this form.')); - $this->config('form_test.object') + $this->configFactory()->get('form_test.object') ->set('bananas', $form_state->getValue('bananas')) ->save(); } diff --git a/core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php b/core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php index de4e6c0..30587df 100644 --- a/core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php +++ b/core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php @@ -54,7 +54,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { drupal_set_message($this->t('The FormTestServiceObject::submitForm() method was used for this form.')); - $this->config('form_test.object') + $this->configFactory()->get('form_test.object') ->set('bananas', $form_state->getValue('bananas')) ->save(); } diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php index ddfbd81..2e07572 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php @@ -722,7 +722,7 @@ public function testDelete() { $config_object->expects($this->once()) ->method('delete'); $configs[] = $config_object; - $config_map[] = array("the_config_prefix.$id", $config_object); + $config_map[] = array("the_config_prefix.$id", TRUE, $config_object); } $this->cacheBackend->expects($this->once()) diff --git a/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php b/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php index c436d6f..cb554fe 100644 --- a/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php @@ -105,41 +105,31 @@ public function testSaveOverride() { $config = $this->getMockBuilder('Drupal\Core\Config\Config') ->disableOriginalConstructor() ->getMock(); + + $definition_test1 = array('parent' => 'test0'); + $definition_test2 = array('parent' => 'test1'); + $config->expects($this->at(0)) - ->method('get') - ->with('definitions') - ->will($this->returnValue(array())); + ->method('getOriginal') + ->with('definitions.test1', FALSE) + ->willReturn(FALSE); $config->expects($this->at(1)) - ->method('get') - ->with('definitions') - ->will($this->returnValue(array())); - - $definition_save_1 = array('definitions' => array('test1' => array('parent' => 'test0'))); - $definitions_save_2 = array( - 'definitions' => array( - 'test1' => array('parent' => 'test0'), - 'test1__la___ma' => array('parent' => 'test1') - ) - ); - $config->expects($this->at(2)) ->method('set') - ->with('definitions', $definition_save_1['definitions']) - ->will($this->returnSelf()); - $config->expects($this->at(3)) + ->with('definitions.test1', $definition_test1) + ->willReturnSelf(); + $config->expects($this->at(2)) ->method('save'); + // Ensure that existing values are merge in and that IDs are massaged to + // work in configuration. + $config->expects($this->at(3)) + ->method('getOriginal') + ->with('definitions.test1__la___ma', FALSE) + ->willReturn(array('weight' => 5)); $config->expects($this->at(4)) - ->method('get') - ->with('definitions') - ->will($this->returnValue($definition_save_1['definitions'])); - $config->expects($this->at(5)) - ->method('get') - ->with('definitions') - ->will($this->returnValue($definition_save_1['definitions'])); - $config->expects($this->at(6)) ->method('set') - ->with('definitions', $definitions_save_2['definitions']) - ->will($this->returnSelf()); - $config->expects($this->at(7)) + ->with('definitions.test1__la___ma', $definition_test2 + array('weight' => 5)) + ->willReturnSelf(); + $config->expects($this->at(5)) ->method('save'); $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); @@ -149,8 +139,11 @@ public function testSaveOverride() { $static_override = new StaticMenuLinkOverrides($config_factory); - $static_override->saveOverride('test1', array('parent' => 'test0')); - $static_override->saveOverride('test1.la__ma', array('parent' => 'test1')); + $properties = $static_override->saveOverride('test1', $definition_test1); + $this->assertEquals(['parent'], $properties); + + $properties = $static_override->saveOverride('test1.la__ma', $definition_test2); + $this->assertEquals(['parent', 'weight'], $properties); } /** @@ -170,8 +163,8 @@ public function testDeleteOverrides($ids, array $old_definitions, array $new_def ->disableOriginalConstructor() ->getMock(); $config->expects($this->at(0)) - ->method('get') - ->with('definitions') + ->method('getOriginal') + ->with('definitions', FALSE) ->will($this->returnValue($old_definitions)); // Only save if the definitions changes. diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index 43748e8..430b468 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -119,7 +119,7 @@ public function getConfigFactoryStub(array $configs = array()) { ->method('get') ->will($this->returnValueMap($map)); - $config_map[] = array($config_name, $config_object); + $config_map[] = array($config_name, TRUE, $config_object); } // Construct a config factory with the array of configuration object stubs // as its return map.