dateFormatter = $date_formatter; $this->requestStack = $request_stack; // Date format depends on field storage format. $definition = $this->getFieldStorageDefinition(); if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) { $this->dateFormat = DATETIME_DATE_STORAGE_FORMAT; } } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('date.formatter'), $container->get('request_stack') ); } /** * Override parent method, which deals with dates as integers. */ 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)); // 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) . "'", $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, $this->dateFormat, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b"); } /** * Override parent method, which deals with dates as integers. */ 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)); // 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) . "'", $this->dateFormat, TRUE); // This is safe because we are manually scrubbing the value. $field = $this->query->getDateFormat($field, $this->dateFormat, TRUE); $this->query->addWhereExpression($this->options['group'], "$field $this->operator $value"); } }