diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index 75f914b..4c32c5f 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -101,8 +101,17 @@ public static function create(ContainerInterface $container, array $configuratio */ protected function opBetween($field) { $origin = ($this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0; - $a = intval(strtotime($this->value['min'], $origin)); - $b = intval(strtotime($this->value['max'], $origin)); + $a = $this->value['min']; + $b = $this->value['max']; + + // Add UTC timezone for not offset 'date' type dates. + if ($this->dateFormat === DATETIME_DATE_STORAGE_FORMAT && $origin === 0) { + $a .= ' ' . DATETIME_STORAGE_TIMEZONE; + $b .= ' ' . DATETIME_STORAGE_TIMEZONE; + } + + $a = intval(strtotime($a, $origin)); + $b = intval(strtotime($b, $origin)); // Convert to ISO format and format for query. UTC timezone is used since // dates are stored in UTC. @@ -120,7 +129,15 @@ protected function opBetween($field) { */ protected function opSimple($field) { $origin = (!empty($this->value['type']) && $this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0; - $value = intval(strtotime($this->value['value'], $origin)); + + $value = $this->value['value']; + + // Add UTC for not offset 'date' type dates. + if ($this->dateFormat === DATETIME_DATE_STORAGE_FORMAT && $origin === 0) { + $value .= ' ' . DATETIME_STORAGE_TIMEZONE; + } + + $value = intval(strtotime($value, $origin)); // Convert to ISO. UTC timezone is used since // dates are stored in UTC. diff --git a/core/modules/datetime/src/Tests/Views/FilterDateTest.php b/core/modules/datetime/src/Tests/Views/FilterDateTest.php index 2a8d7cd..792acd4 100644 --- a/core/modules/datetime/src/Tests/Views/FilterDateTest.php +++ b/core/modules/datetime/src/Tests/Views/FilterDateTest.php @@ -107,4 +107,43 @@ public function testDateOffsets() { $this->assertIdenticalResultset($view, $expected_result, $this->map); } + /** + * Test date filter with date-only fields. + */ + public function testDateIs() { + $view = Views::getView('test_filter_datetime'); + $field = static::$field_name . '_value'; + + // Test simple operations. + $view->initHandlers(); + + // Filtering with nodes date-only values (format: Y-m-d) to test + // UTC conversion does NOT change the day. + $view->filter[$field]->operator = '='; + $view->filter[$field]->value['type'] = 'date'; + $view->filter[$field]->value['value'] = $this->nodes[2]->field_date->first()->getValue()['value']; + $view->setDisplay('default'); + $this->executeView($view); + $expected_result = [ + ['nid' => $this->nodes[2]->id()], + ]; + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + + // Test offset for between operator. Only 'today' and 'tomorrow' node should appear. + $view->initHandlers(); + $view->filter[$field]->operator = 'between'; + $view->filter[$field]->value['type'] = 'date'; + $view->filter[$field]->value['max'] = $this->nodes[0]->field_date->first()->getValue()['value']; + $view->filter[$field]->value['min'] = $this->nodes[1]->field_date->first()->getValue()['value']; + $view->setDisplay('default'); + $this->executeView($view); + $expected_result = [ + ['nid' => $this->nodes[0]->id()], + ['nid' => $this->nodes[1]->id()], + ]; + $this->assertIdenticalResultset($view, $expected_result, $this->map); + } + }