diff --git a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php index dade1df..b31e32b 100644 --- a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php @@ -1325,15 +1325,20 @@ public function storeGroupInput($input, $status) { } /** - * Check to see if input from the exposed filters should change - * the behavior of this filter. + * Determines if the input from a filter should change the generated query. + * + * @param array $input + * The exposed data for this view. + * + * @return bool + * TRUE if the input for this filter should be included in the view query. + * FALSE otherwise. */ public function acceptExposedInput($input) { if (empty($this->options['exposed'])) { return TRUE; } - if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) { $this->operator = $input[$this->options['expose']['operator_id']]; } @@ -1351,6 +1356,17 @@ public function acceptExposedInput($input) { if ($value == 'All' || $value === array()) { return FALSE; } + + // Checkboxes show up as an array in the form of option_id => bool. + // If all the values are zero, then there is no input. + if (is_array($value)) { + $checked = array_filter($value, function($value) { + return $value !== 0; + }); + if (empty($checked)) { + return FALSE; + } + } } if (!empty($this->alwaysMultiple) && $value === '') { diff --git a/core/modules/views/src/Tests/Plugin/ExposedFormTest.php b/core/modules/views/src/Tests/Plugin/ExposedFormTest.php index 1c520e0..fbd5b88 100644 --- a/core/modules/views/src/Tests/Plugin/ExposedFormTest.php +++ b/core/modules/views/src/Tests/Plugin/ExposedFormTest.php @@ -148,6 +148,38 @@ public function testExposedFormRender() { } /** + * Tests overriding the default render option with checkboxes. + */ + public function testExposedFormRenderCheckboxes() { + // Make sure we have at least two options for node type. + $this->drupalCreateContentType(['type' => 'page']); + $this->drupalCreateNode(['type' => 'page']); + + // Use a test theme to convert multi-select elements into checkboxes. + \Drupal::service('theme_handler')->install(array('views_test_checkboxes_theme')); + $this->config('system.theme') + ->set('default', 'views_test_checkboxes_theme') + ->save(); + + // Set the "type" filter to multi-select. + $view = Views::getView('test_exposed_form_buttons'); + $filter = $view->getHandler('page_1', 'filter', 'type'); + $filter['expose']['multiple'] = TRUE; + $view->setHandler('page_1', 'filter', 'type', $filter); + $view->save(); + $this->drupalGet('test_exposed_form_buttons'); + + $actual = $this->xpath('//form//input[@type="checkbox" and @name="type[article]"]'); + $this->assertEqual(count($actual), 1, 'Article option renders as a checkbox.'); + $actual = $this->xpath('//form//input[@type="checkbox" and @name="type[page]"]'); + $this->assertEqual(count($actual), 1, 'Page option renders as a checkbox'); + + // Ensure that all results are displayed. + $rows = $this->xpath("//div[contains(@class, 'views-row')]"); + $this->assertEqual(count($rows), 6, 'All rows are displayed by default when no options are checked.'); + } + + /** * Tests the exposed block functionality. */ public function testExposedBlock() { diff --git a/core/modules/views/tests/themes/views_test_checkboxes_theme/views_test_checkboxes_theme.info.yml b/core/modules/views/tests/themes/views_test_checkboxes_theme/views_test_checkboxes_theme.info.yml new file mode 100644 index 0000000..50bf73e --- /dev/null +++ b/core/modules/views/tests/themes/views_test_checkboxes_theme/views_test_checkboxes_theme.info.yml @@ -0,0 +1,5 @@ +name: Views test checkboxes theme +type: theme +description: Theme for testing Views rendering of checkboxes. +version: VERSION +core: 8.x diff --git a/core/modules/views/tests/themes/views_test_checkboxes_theme/views_test_checkboxes_theme.theme b/core/modules/views/tests/themes/views_test_checkboxes_theme/views_test_checkboxes_theme.theme new file mode 100644 index 0000000..4a038ae --- /dev/null +++ b/core/modules/views/tests/themes/views_test_checkboxes_theme/views_test_checkboxes_theme.theme @@ -0,0 +1,7 @@ +