Loading core/modules/views/src/Plugin/views/filter/Date.php +15 −0 Original line number Diff line number Diff line Loading @@ -172,6 +172,13 @@ public function acceptExposedInput($input) { // When the operator is either <, <=, =, !=, >=, > or regular_expression // the input contains only one value. if ($this->value['value'] == '') { // If the filter was not submitted but has a default offset value, // apply it so backends receive a resolved date. if (!empty($this->options['value']['value']) && $this->options['value']['type'] === 'offset') { $this->value['value'] = $this->options['value']['value']; $this->value['type'] = 'offset'; return TRUE; } return FALSE; } } Loading @@ -179,6 +186,14 @@ public function acceptExposedInput($input) { // When the operator is either between or not between the input contains // two values. if ($this->value['min'] == '' || $this->value['max'] == '') { // If the filter was not submitted but has default offset values, // apply them so backends receive resolved dates. if ((!empty($this->options['value']['min']) || !empty($this->options['value']['max'])) && $this->options['value']['type'] === 'offset') { $this->value['min'] = $this->options['value']['min']; $this->value['max'] = $this->options['value']['max']; $this->value['type'] = 'offset'; return TRUE; } return FALSE; } } Loading core/modules/views/tests/src/Functional/Handler/FilterDateTest.php +71 −0 Original line number Diff line number Diff line Loading @@ -361,4 +361,75 @@ public function testExposedFilter(): void { $this->assertSession()->fieldExists('created'); } /** * Tests an exposed date filter with an offset default value. * * Verify that results are filtered on page load without requiring form * submission. */ public function testExposedOffsetDefaultAppliedOnPageLoad(): void { $this->drupalLogin($this->drupalCreateUser(['administer views'])); // Expose the filter with operator > and default offset "now". $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created'); $this->submitForm([], 'Expose filter'); $edit = [ 'options[operator]' => '>', 'options[value][type]' => 'offset', 'options[value][value]' => 'now', ]; $this->submitForm($edit, 'Apply'); // Add a page display and save. $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit'); $this->submitForm([], 'Add Page'); $this->drupalGet('admin/structure/views/nojs/display/test_filter_date_between/page_1/path'); $this->submitForm(['path' => 'exposed-offset-default'], 'Apply'); $this->submitForm([], 'Save'); // On page load (no form submission), only the future node should appear. $this->drupalGet('exposed-offset-default'); $results = $this->cssSelect('.view-content .field-content'); $this->assertCount(1, $results); $this->assertEquals($this->nodes[3]->id(), $results[0]->getText()); } /** * Tests an exposed date filter with offset default value (between operator). * * Verify that results are filtered on page load without requiring form * submission. */ public function testExposedOffsetBetweenDefaultAppliedOnPageLoad(): void { $this->drupalLogin($this->drupalCreateUser(['administer views'])); // Expose the filter with operator "between" and offset defaults. $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created'); $this->submitForm([], 'Expose filter'); $edit = [ 'options[operator]' => 'between', 'options[value][type]' => 'offset', 'options[value][min]' => 'now', 'options[value][max]' => '+2 days', ]; $this->submitForm($edit, 'Apply'); // Add a page display and save. $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit'); $this->submitForm([], 'Add Page'); $this->drupalGet('admin/structure/views/nojs/display/test_filter_date_between/page_1/path'); $this->submitForm(['path' => 'exposed-offset-between-default'], 'Apply'); $this->submitForm([], 'Save'); // Add a node beyond the max offset (+2 days) to verify it is excluded. $this->nodes[] = $this->drupalCreateNode(['created' => time() + (86400 * 3)]); // On page load (no form submission), only the node within [now, +2 days] // should appear; the node beyond +2 days must be excluded. $this->drupalGet('exposed-offset-between-default'); $results = $this->cssSelect('.view-content .field-content'); $this->assertCount(1, $results); $this->assertEquals($this->nodes[3]->id(), $results[0]->getText()); } } Loading
core/modules/views/src/Plugin/views/filter/Date.php +15 −0 Original line number Diff line number Diff line Loading @@ -172,6 +172,13 @@ public function acceptExposedInput($input) { // When the operator is either <, <=, =, !=, >=, > or regular_expression // the input contains only one value. if ($this->value['value'] == '') { // If the filter was not submitted but has a default offset value, // apply it so backends receive a resolved date. if (!empty($this->options['value']['value']) && $this->options['value']['type'] === 'offset') { $this->value['value'] = $this->options['value']['value']; $this->value['type'] = 'offset'; return TRUE; } return FALSE; } } Loading @@ -179,6 +186,14 @@ public function acceptExposedInput($input) { // When the operator is either between or not between the input contains // two values. if ($this->value['min'] == '' || $this->value['max'] == '') { // If the filter was not submitted but has default offset values, // apply them so backends receive resolved dates. if ((!empty($this->options['value']['min']) || !empty($this->options['value']['max'])) && $this->options['value']['type'] === 'offset') { $this->value['min'] = $this->options['value']['min']; $this->value['max'] = $this->options['value']['max']; $this->value['type'] = 'offset'; return TRUE; } return FALSE; } } Loading
core/modules/views/tests/src/Functional/Handler/FilterDateTest.php +71 −0 Original line number Diff line number Diff line Loading @@ -361,4 +361,75 @@ public function testExposedFilter(): void { $this->assertSession()->fieldExists('created'); } /** * Tests an exposed date filter with an offset default value. * * Verify that results are filtered on page load without requiring form * submission. */ public function testExposedOffsetDefaultAppliedOnPageLoad(): void { $this->drupalLogin($this->drupalCreateUser(['administer views'])); // Expose the filter with operator > and default offset "now". $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created'); $this->submitForm([], 'Expose filter'); $edit = [ 'options[operator]' => '>', 'options[value][type]' => 'offset', 'options[value][value]' => 'now', ]; $this->submitForm($edit, 'Apply'); // Add a page display and save. $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit'); $this->submitForm([], 'Add Page'); $this->drupalGet('admin/structure/views/nojs/display/test_filter_date_between/page_1/path'); $this->submitForm(['path' => 'exposed-offset-default'], 'Apply'); $this->submitForm([], 'Save'); // On page load (no form submission), only the future node should appear. $this->drupalGet('exposed-offset-default'); $results = $this->cssSelect('.view-content .field-content'); $this->assertCount(1, $results); $this->assertEquals($this->nodes[3]->id(), $results[0]->getText()); } /** * Tests an exposed date filter with offset default value (between operator). * * Verify that results are filtered on page load without requiring form * submission. */ public function testExposedOffsetBetweenDefaultAppliedOnPageLoad(): void { $this->drupalLogin($this->drupalCreateUser(['administer views'])); // Expose the filter with operator "between" and offset defaults. $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created'); $this->submitForm([], 'Expose filter'); $edit = [ 'options[operator]' => 'between', 'options[value][type]' => 'offset', 'options[value][min]' => 'now', 'options[value][max]' => '+2 days', ]; $this->submitForm($edit, 'Apply'); // Add a page display and save. $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit'); $this->submitForm([], 'Add Page'); $this->drupalGet('admin/structure/views/nojs/display/test_filter_date_between/page_1/path'); $this->submitForm(['path' => 'exposed-offset-between-default'], 'Apply'); $this->submitForm([], 'Save'); // Add a node beyond the max offset (+2 days) to verify it is excluded. $this->nodes[] = $this->drupalCreateNode(['created' => time() + (86400 * 3)]); // On page load (no form submission), only the node within [now, +2 days] // should appear; the node beyond +2 days must be excluded. $this->drupalGet('exposed-offset-between-default'); $results = $this->cssSelect('.view-content .field-content'); $this->assertCount(1, $results); $this->assertEquals($this->nodes[3]->id(), $results[0]->getText()); } }