diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index c6200bc16a9671cf9e0e1e1bfff6d4694c858bb1..53a9ac037c68abaa1c53098583104c13be5a2b2c 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -326,8 +326,14 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { */ public function submitOptionsForm(&$form, FormStateInterface $form_state) { $element = ['#parents' => ['query', 'options', 'query_tags']]; - $value = explode(',', NestedArray::getValue($form_state->getValues(), $element['#parents'])); - $value = array_filter(array_map('trim', $value)); + $value = NestedArray::getValue($form_state->getValues(), $element['#parents']); + // When toggling a display to override defaults or vice-versa the submit + // handler gets invoked twice, and we don't want to bash the values from the + // original call. + if (is_array($value)) { + return; + } + $value = array_filter(array_map('trim', explode(',', $value))); $form_state->setValueForElement($element, $value); } diff --git a/core/modules/views/tests/src/Functional/Plugin/QueryOptionsTest.php b/core/modules/views/tests/src/Functional/Plugin/QueryOptionsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..cb027c06e12c5f2e5bbb1aaaabc38be33b9bdb45 --- /dev/null +++ b/core/modules/views/tests/src/Functional/Plugin/QueryOptionsTest.php @@ -0,0 +1,70 @@ +<?php + +namespace Drupal\Tests\views\Functional\Plugin; + +use Drupal\Tests\views\Functional\ViewTestBase; + +/** + * Tests setting the query options. + * + * @group views + */ +class QueryOptionsTest extends ViewTestBase { + + /** + * Views used by this test. + * + * @var array + */ + public static $testViews = ['test_view']; + + /** + * Modules to enable. + * + * @var array + */ + protected static $modules = ['node', 'views_ui']; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * Test that query overrides are stored. + */ + public function testStoreQuerySettingsOverride() { + // Show the default display so the override selection is shown. + \Drupal::configFactory()->getEditable('views.settings')->set('ui.show.default_display', TRUE)->save(); + + $admin_user = $this->drupalCreateUser([ + 'administer views', + 'administer site configuration', + ]); + $this->drupalLogin($admin_user); + + $edit = []; + $this->drupalGet('admin/structure/views/view/test_view/edit'); + $this->submitForm($edit, 'Add Page'); + + $this->drupalGet('admin/structure/views/nojs/display/test_view/page_1/query'); + $this->assertSession()->checkboxNotChecked('query[options][distinct]'); + $edit = [ + 'override[dropdown]' => 'page_1', + 'query[options][distinct]' => 1, + ]; + $this->submitForm($edit, 'Apply'); + $this->drupalGet('admin/structure/views/nojs/display/test_view/page_1/query'); + $this->assertSession()->checkboxChecked('query[options][distinct]'); + $edit = [ + 'query[options][query_comment]' => 'comment', + 'query[options][query_tags]' => 'query_tag, another_tag', + ]; + $this->submitForm($edit, 'Apply'); + $this->drupalGet('admin/structure/views/nojs/display/test_view/page_1/query'); + $this->assertSession()->checkboxChecked('query[options][distinct]'); + $this->assertSession()->fieldValueEquals('query[options][query_comment]', 'comment'); + $this->assertSession()->fieldValueEquals('query[options][query_tags]', 'query_tag, another_tag'); + } + +}