diff --git a/src/Form/CheckboxWidgetForm.php b/src/Form/CheckboxWidgetForm.php index 33beff9..230b07c 100644 --- a/src/Form/CheckboxWidgetForm.php +++ b/src/Form/CheckboxWidgetForm.php @@ -121,15 +121,26 @@ class CheckboxWidgetForm implements BaseFormIdInterface { } // The form was submitted but nothing was active in the form, we should - // still redirect, but the url for the new page can't come from a result. - // So we're redirecting to the facet source's page. - $path = $facet->getFacetSource()->getPath(); - if (substr($path, 0, 1) !== '/') { - $path = '/' . $path; + // still redirect, but remove the parameters for the current facet from the + // request. + $cr = \Drupal::requestStack()->getCurrentRequest(); + $request_uri = $cr->getRequestUri(); + $base_request_uri = substr($request_uri, 0, strpos($request_uri, '?')); + $qs = $cr->getQueryString(); + $query_parts = explode('&', $qs); + foreach ($query_parts as $i => $part) { + if (strpos($part, $facet->getUrlAlias().'%3A') !== FALSE) { + unset($query_parts[$i]); + } } - $link = Url::fromUserInput($path); - $link->setAbsolute(); - $form_state->setResponse(new RedirectResponse($link->toString())); + + if (count($query_parts) === 0) { + $form_state->setResponse(new RedirectResponse($base_request_uri)); + return; + } + + $url = $base_request_uri . '?' . implode('&', $query_parts); + $form_state->setResponse(new RedirectResponse($url)); } } diff --git a/src/Tests/WidgetIntegrationTest.php b/src/Tests/WidgetIntegrationTest.php index 5f0a323..8710423 100644 --- a/src/Tests/WidgetIntegrationTest.php +++ b/src/Tests/WidgetIntegrationTest.php @@ -76,6 +76,72 @@ class WidgetIntegrationTest extends WebTestBase { } /** + * Tests submitting multiple items at the same time. + */ + public function testMultipleCheckboxItems() { + $id = 'palau'; + $name = 'Palau owl'; + $facet_add_page = 'admin/config/search/facets/add-facet'; + + // Create a new facet. + $this->drupalGet($facet_add_page); + $form_values = [ + 'id' => $id, + 'status' => 1, + 'url_alias' => $id, + 'name' => $name, + 'weight' => 7, + 'facet_source_id' => 'search_api_views:search_api_test_view:page_1', + 'facet_source_configs[search_api_views:search_api_test_view:page_1][field_identifier]' => 'keywords', + ]; + $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_view:page_1'], $this->t('Configure facet source')); + $this->drupalPostForm(NULL, $form_values, $this->t('Save')); + $this->drupalPostForm(NULL, ['widget' => 'checkbox'], $this->t('Save')); + + // Place a block. + $block_values = [ + 'plugin_id' => 'facet_block:' . $id, + 'settings' => [ + 'region' => 'footer', + 'id' => str_replace('_', '-', $id), + ], + ]; + $this->drupalPlaceBlock($block_values['plugin_id'], $block_values['settings']); + + // Go to the view, check that all results are displayed. + $this->drupalGet('search-api-test-fulltext'); + $this->assertText('Displaying 5 search results'); + + // Filter by the grape keyword. + $edit = ['keywords[grape]' => 'grape']; + $this->drupalPostForm(NULL, $edit, $this->t('submit')); + $this->assertText('Displaying 3 search results'); + $this->assertFieldChecked('edit-keywords-grape'); + + // Add apple to the filter as well. + $edit = ['keywords[grape]' => 'grape', 'keywords[apple]' => 'apple']; + $this->drupalPostForm(NULL, $edit, $this->t('submit')); + $this->assertText('Displaying 3 search results'); + $this->assertFieldChecked('edit-keywords-apple'); + $this->assertFieldChecked('edit-keywords-grape'); + + // Reset the filters back to default and make sure that the original 5 + // results are displayed again. + $this->drupalPostForm(NULL, [], $this->t('submit')); + $this->assertText('Displaying 5 search results'); + $this->assertNoFieldChecked('edit-keywords-apple'); + $this->assertNoFieldChecked('edit-keywords-grape'); + + // Filter on both grape and apple at once and check that those are now + // checked. + $edit = ['keywords[grape]' => 'grape', 'keywords[apple]' => 'apple']; + $this->drupalPostForm(NULL, $edit, $this->t('submit')); + $this->assertText('Displaying 3 search results'); + $this->assertFieldChecked('edit-keywords-apple'); + $this->assertFieldChecked('edit-keywords-grape'); + } + + /** * Tests multiple checkbox widgets. */ public function testMultipleCheckboxWidget() {