diff --git a/core/modules/datetime/src/Plugin/views/argument/Date.php b/core/modules/datetime/src/Plugin/views/argument/Date.php index c59930a..d6b933b 100644 --- a/core/modules/datetime/src/Plugin/views/argument/Date.php +++ b/core/modules/datetime/src/Plugin/views/argument/Date.php @@ -33,4 +33,14 @@ public function getDateField() { // Return the real field, since it is already in string format. return "$this->tableAlias.$this->realField"; } + + /** + * Override to account for dates stored as strings. + * + * {@inheritdoc} + */ + public function getDateFormat($format) { + // Pass in the string-field option. + return $this->query->getDateFormat($this->getDateField(), $format, TRUE); + } } diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index 0517fe3..9af0fd6 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -43,12 +43,12 @@ protected function opBetween($field) { $b = REQUEST_TIME + $b; } // Convert to ISO format and format for query. - $a = $this->query->getDateFormat("'" . format_date($a, 'custom', 'c') . "'", static::$dateFormat); - $b = $this->query->getDateFormat("'" . format_date($b, 'custom', 'c') . "'", static::$dateFormat); + $a = $this->query->getDateFormat("'" . format_date($a, 'custom', 'c') . "'", static::$dateFormat, TRUE); + $b = $this->query->getDateFormat("'" . format_date($b, 'custom', 'c') . "'", static::$dateFormat, TRUE); // This is safe because we are manually scrubbing the values. $operator = strtoupper($this->operator); - $field = $this->query->getDateFormat($field, static::$dateFormat); + $field = $this->query->getDateFormat($field, static::$dateFormat, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b"); } @@ -61,10 +61,10 @@ protected function opSimple($field) { $value = REQUEST_TIME + $value; } // Convert to ISO. - $value = $this->query->getDateFormat("'" . format_date($value, 'custom', 'c') . "'", static::$dateFormat); + $value = $this->query->getDateFormat("'" . format_date($value, 'custom', 'c') . "'", static::$dateFormat, TRUE); // This is safe because we are manually scrubbing the value. - $field = $this->query->getDateFormat($field, static::$dateFormat); + $field = $this->query->getDateFormat($field, static::$dateFormat, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $this->operator $value"); } diff --git a/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_filter_datetime.yml b/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_filter_datetime.yml index f1210d0..4b3fea8 100644 --- a/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_filter_datetime.yml +++ b/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_filter_datetime.yml @@ -32,6 +32,14 @@ display: table: node__field_date field: field_date_value plugin_id: datetime + sorts: + id: + field: nid + id: nid + order: ASC + relationship: none + table: node + plugin_id: numeric pager: type: full query: diff --git a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php index 98e7e50..8538f9a 100644 --- a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php +++ b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php @@ -235,12 +235,15 @@ public function setupTimezone() { * An appropriate query expression pointing to the date field. * @param string $format * A format string for the result, like 'Y-m-d H:i:s'. + * @param boolean $string_date + * For certain databases, date format functions vary depending on string or + * numeric storage. * * @return string * A string representing the field formatted as a date in the format * specified by $format. */ - public function getDateFormat($field, $format) { + public function getDateFormat($field, $format, $string_date = FALSE) { return $field; } diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index 3aed415..16ee5b9 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -1676,9 +1676,9 @@ public function setupTimezone() { } /** - * Overrides \Drupal\views\Plugin\views\query\QueryPluginBase::getDateFormat(). + * {@inheritdoc} */ - public function getDateFormat($field, $format) { + public function getDateFormat($field, $format, $string_date = FALSE) { $db_type = Database::getConnection()->databaseType(); switch ($db_type) { case 'mysql': @@ -1725,7 +1725,13 @@ public function getDateFormat($field, $format) { 'A' => 'AM', ); $format = strtr($format, $replace); - return "TO_CHAR($field, '$format')"; + if (!$string_date) { + return "TO_CHAR($field, '$format')"; + } + // In order to allow for partials (eg, only the year), transform to a + // date, back to a string again. + // @todo this is very messy, and EXTRACT should probably be used. + return "TO_CHAR(TO_TIMESTAMP($field, 'YYYY-MM-DD HH24:MI:SS'), '$format')"; case 'sqlite': $replace = array( 'Y' => '%Y',