diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index 6fe2c30..56b3bf6 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -9,6 +9,8 @@ use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; +use Drupal\views\FieldAPIHandlerTrait; use Drupal\views\Plugin\views\filter\Date as NumericDate; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -24,6 +26,8 @@ */ class Date extends NumericDate implements ContainerFactoryPluginInterface { + use FieldAPIHandlerTrait; + /** * The date formatter service. * @@ -32,6 +36,15 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface { protected $dateFormatter; /** + * Date format for SQL conversion. + * + * @var string + * + * @see \Drupal\views\Plugin\views\query\Sql::getDateFormat() + */ + protected $dateFormat = 'Y-m-d H:i:s'; + + /** * Constructs a new Date handler. * * @param array $configuration @@ -46,6 +59,12 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface { public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatter $date_formatter) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->dateFormatter = $date_formatter; + + // Date format depends on field storage format. + $definition = $this->getFieldStorageDefinition(); + if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) { + $this->dateFormat = 'Y-m-d'; + } } /** @@ -61,15 +80,6 @@ public static function create(ContainerInterface $container, array $configuratio } /** - * Date format for SQL conversion. - * - * @var string - * - * @see \Drupal\views\Plugin\views\query\Sql::getDateFormat() - */ - protected static $dateFormat = 'Y-m-d H:i:s'; - - /** * Override parent method, which deals with dates as integers. */ protected function opBetween($field) { @@ -77,14 +87,17 @@ protected function opBetween($field) { $a = intval(strtotime($this->value['min'], $origin)); $b = intval(strtotime($this->value['max'], $origin)); + // Formatting will vary on date storage. + + // Convert to ISO format and format for query. UTC timezone is used since // dates are stored in UTC. - $a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", static::$dateFormat, TRUE); - $b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", static::$dateFormat, TRUE); + $a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE); + $b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, 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, $this->dateFormat, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b"); } @@ -96,10 +109,10 @@ protected function opSimple($field) { $value = intval(strtotime($this->value['value'], $origin)); // Convert to ISO. UTC is used since dates are stored in UTC. - $value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", static::$dateFormat, TRUE); + $value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE); // This is safe because we are manually scrubbing the value. - $field = $this->query->getDateFormat($field, static::$dateFormat, TRUE); + $field = $this->query->getDateFormat($field, $this->dateFormat, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $this->operator $value"); }