diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index df542b0..9e9dfcc 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -29,12 +29,61 @@ class Date extends NumericDate { * * @see \Drupal\views\Plugin\views\query\Sql::getDateFormat() */ - protected static $dateFormat = 'Y-m-d H:i:s'; + protected static $dateFormat = 'Y-m-d'; + + /** + * Datetime format for SQL conversion. + * + * @var string + * + * @see \Drupal\views\Plugin\views\query\Sql::getDateFormat() + */ + protected static $dateTimeFormat = 'Y-m-d H:i:s'; + + /** + * Add compare by date options default value. + */ + protected function defineOptions() { + $options = parent::defineOptions(); + + $options['value']['contains']['compare_by_date']['default'] = FALSE; + + return $options; + } + + /** + * Add a compare by date type checkob to the value form. + */ + protected function valueForm(&$form, FormStateInterface $form_state) { + parent::valueForm($form, $form_state); + + if (!$form_state->get('exposed')) { + $form['value']['compare_by_date'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Compare only date part'), + '#description' => $this->t('Should we compare just date part or also time part?'), + '#default_value' => !empty($this->value['compare_by_date']) ? $this->value['compare_by_date'] : FALSE, + ); + } + + // Change text field date to real date time control. + $type = $this->value['compare_by_date'] ? 'date' : 'datetime'; + if (!empty($form['value']['value'])) { + $form['value']['value']['#type'] = $type; + } + if (!empty($form['value']['min'])) { + $form['value']['min']['#type'] = $type; + $form['value']['max']['#type'] = $type; + } + + } /** * Override parent method, which deals with dates as integers. */ protected function opBetween($field) { + $date_format = $this->options['value']['compare_by_date'] ? static::$dateFormat : static::$dateTimeFormat; + $a = intval(strtotime($this->value['min'], 0)); $b = intval(strtotime($this->value['max'], 0)); @@ -43,12 +92,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, TRUE); - $b = $this->query->getDateFormat("'" . format_date($b, 'custom', 'c') . "'", static::$dateFormat, TRUE); + $a = $this->query->getDateFormat("'" . format_date($a, 'custom', 'c') . "'", $date_format, TRUE); + $b = $this->query->getDateFormat("'" . format_date($b, 'custom', 'c') . "'", $date_format, TRUE); // This is safe because we are manually scrubbing the values. $operator = strtoupper($this->operator); - $field = $this->query->getDateFormat($field, static::$dateFormat, TRUE); + $field = $this->query->getDateFormat($field, $date_format, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b"); } @@ -56,15 +105,17 @@ protected function opBetween($field) { * Override parent method, which deals with dates as integers. */ protected function opSimple($field) { + $date_format = $this->options['value']['compare_by_date'] ? static::$dateFormat : static::$dateTimeFormat; + $value = intval(strtotime($this->value['value'], 0)); if (!empty($this->value['type']) && $this->value['type'] == 'offset') { $value = REQUEST_TIME + $value; } // Convert to ISO. - $value = $this->query->getDateFormat("'" . format_date($value, 'custom', 'c') . "'", static::$dateFormat, TRUE); + $value = $this->query->getDateFormat("'" . format_date($value, 'custom', 'c') . "'", $date_format, TRUE); // This is safe because we are manually scrubbing the value. - $field = $this->query->getDateFormat($field, static::$dateFormat, TRUE); + $field = $this->query->getDateFormat($field, $date_format, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $this->operator $value"); }