View source
<?php
namespace Drupal\datetime\Plugin\views\filter;
use Drupal\Core\Datetime\DateFormatterInterface;
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;
use Symfony\Component\HttpFoundation\RequestStack;
class Date extends NumericDate implements ContainerFactoryPluginInterface {
use FieldAPIHandlerTrait;
protected $dateFormatter;
protected $dateFormat = DATETIME_DATETIME_STORAGE_FORMAT;
protected $requestStack;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->dateFormatter = $date_formatter;
$this->requestStack = $request_stack;
$definition = $this
->getFieldStorageDefinition();
if ($definition
->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
$this->dateFormat = DATETIME_DATE_STORAGE_FORMAT;
}
}
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'));
}
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->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);
$operator = strtoupper($this->operator);
$field = $this->query
->getDateFormat($field, $this->dateFormat, TRUE);
$this->query
->addWhereExpression($this->options['group'], "{$field} {$operator} {$a} AND {$b}");
}
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->query
->getDateFormat("'" . $this->dateFormatter
->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
$field = $this->query
->getDateFormat($field, $this->dateFormat, TRUE);
$this->query
->addWhereExpression($this->options['group'], "{$field} {$this->operator} {$value}");
}
}