diff --git a/core/modules/views/config/schema/views.filter.schema.yml b/core/modules/views/config/schema/views.filter.schema.yml index 3a97c25..964c46c 100644 --- a/core/modules/views/config/schema/views.filter.schema.yml +++ b/core/modules/views/config/schema/views.filter.schema.yml @@ -129,6 +129,13 @@ views.filter.group_item.numeric: value: type: views.filter_value.numeric +views.filter.group_item.date: + type: views_filter_group_item + label: 'Group items' + mapping: + value: + type: views.filter_value.date + # Schema for the views filter value. views.filter_value.boolean: diff --git a/core/modules/views/src/Plugin/views/filter/Date.php b/core/modules/views/src/Plugin/views/filter/Date.php index 7e5417f..1944e1f 100644 --- a/core/modules/views/src/Plugin/views/filter/Date.php +++ b/core/modules/views/src/Plugin/views/filter/Date.php @@ -136,7 +136,12 @@ public function acceptExposedInput($input) { } // Store this because it will get overwritten. - $type = $this->value['type']; + if ($this->isAGroup()) { + $type = $this->group_info['type']; + } + else { + $type = $this->value['type']; + } $rc = parent::acceptExposedInput($input); // Don't filter if value(s) are empty. diff --git a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php index 3af0e9c..9fd50de 100644 --- a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php @@ -990,17 +990,20 @@ protected function buildExposedFiltersGroupForm(&$form, FormStateInterface $form $children = Element::children($row['value']); if (!empty($children)) { foreach ($children as $child) { - foreach ($row['value'][$child]['#states']['visible'] as $state) { - if (isset($state[':input[name="options[group_info][group_items][' . $item_id . '][operator]"]'])) { - $row['value'][$child]['#title'] = ''; + if (!empty($row['value'][$child]['#states']['visible'])) { + foreach ($row['value'][$child]['#states']['visible'] as $state) { + if (isset($state[':input[name="options[group_info][group_items][' . $item_id . '][operator]"]'])) { + $row['value'][$child]['#title'] = ''; - if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$child])) { - $row['value'][$child]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$child]; + // Exit this loop and process the next child element. + break; } - // Exit this loop and process the next child element. - break; } } + + if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$child])) { + $row['value'][$child]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$child]; + } } } else { diff --git a/core/modules/views/src/Tests/Handler/FilterDateTest.php b/core/modules/views/src/Tests/Handler/FilterDateTest.php index c538bc3..8dd88f9 100644 --- a/core/modules/views/src/Tests/Handler/FilterDateTest.php +++ b/core/modules/views/src/Tests/Handler/FilterDateTest.php @@ -51,6 +51,7 @@ protected function testDateFilter() { $this->_testOffset(); $this->_testBetween(); $this->_testUiValidation(); + $this->_testFilterDateUI(); } /** @@ -158,4 +159,60 @@ protected function _testUiValidation() { $this->assertText(t('Invalid date format.'), 'Make sure that validation is runned and the invalidate date format is identified.'); } + /** + * Test grouped date filter. + */ + protected function _testFilterDateUI() { + $this->drupalLogin($this->drupalCreateUser(array('administer views'))); + $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created'); + $this->drupalPostForm(NULL, array(), t('Expose filter')); + $this->drupalPostForm(NULL, array(), t('Grouped filters')); + + $edit = array(); + $edit['options[group_info][group_items][1][title]'] = 'simple-offset'; + $edit['options[group_info][group_items][1][operator]'] = '>'; + $edit['options[group_info][group_items][1][value][type]'] = 'offset'; + $edit['options[group_info][group_items][1][value][value]'] = '+1 hour'; + $edit['options[group_info][group_items][2][title]'] = 'between-offset'; + $edit['options[group_info][group_items][2][operator]'] = 'between'; + $edit['options[group_info][group_items][2][value][type]'] = 'offset'; + $edit['options[group_info][group_items][2][value][min]'] = '+1 hour'; + $edit['options[group_info][group_items][2][value][max]'] = '+2 days'; + $edit['options[group_info][group_items][3][title]'] = 'between-date'; + $edit['options[group_info][group_items][3][operator]'] = 'between'; + $edit['options[group_info][group_items][3][value][min]'] = format_date(150000, 'custom', 'Y-m-d H:s'); + $edit['options[group_info][group_items][3][value][max]'] = format_date(250000, 'custom', 'Y-m-d H:s'); + + $this->drupalPostForm(NULL, $edit, t('Apply')); + + $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created'); + foreach ($edit as $name => $value) { + $this->assertFieldByName($name, $value); + if (strpos($name, '[value][type]')) { + $radio = $this->cssSelect('input[name="' . $name . '"][checked="checked"][type="radio"]'); + $this->assertEqual((string) $radio[0]['value'], $value); + } + } + + // Test that the exposed filter works as expected. + $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit'); + $this->drupalPostForm(NULL, array(), t('Update preview')); + $results = $this->cssSelect('.view-content .field-content'); + $this->assertEqual(count($results), 4); + $this->drupalPostForm(NULL, array('created' => '1'), t('Update preview')); + $results = $this->cssSelect('.view-content .field-content'); + $this->assertEqual(count($results), 1); + $this->assertEqual((string) $results[0], $this->nodes[3]->id()); + $this->drupalPostForm(NULL, array('created' => '2'), t('Update preview')); + $results = $this->cssSelect('.view-content .field-content'); + $this->assertEqual(count($results), 1); + $this->assertEqual((string) $results[0], $this->nodes[3]->id()); + $this->drupalPostForm(NULL, array('created' => '3'), t('Update preview')); + $results = $this->cssSelect('.view-content .field-content'); + $this->assertEqual(count($results), 1); + $this->assertEqual((string) $results[0], $this->nodes[1]->id()); + + $this->drupalPostForm('admin/structure/views/view/test_filter_date_between', array(), t('Save')); + } + }