3 namespace Drupal\datetime\Plugin\views\filter;
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
8 use Drupal\views\FieldAPIHandlerTrait;
9 use Drupal\views\Plugin\views\filter\Date as NumericDate;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RequestStack;
14 * Date/time views filter.
16 * Even thought dates are stored as strings, the numeric filter is extended
17 * because it provides more sensible operators.
19 * @ingroup views_filter_handlers
21 * @ViewsFilter("datetime")
23 class Date extends NumericDate implements ContainerFactoryPluginInterface {
25 use FieldAPIHandlerTrait;
28 * The date formatter service.
30 * @var \Drupal\Core\Datetime\DateFormatterInterface
32 protected $dateFormatter;
35 * Date format for SQL conversion.
39 * @see \Drupal\views\Plugin\views\query\Sql::getDateFormat()
41 protected $dateFormat = DATETIME_DATETIME_STORAGE_FORMAT;
44 * The request stack used to determin current time.
46 * @var \Symfony\Component\HttpFoundation\RequestStack
48 protected $requestStack;
51 * Constructs a new Date handler.
53 * @param array $configuration
54 * A configuration array containing information about the plugin instance.
55 * @param string $plugin_id
56 * The plugin ID for the plugin instance.
57 * @param mixed $plugin_definition
58 * The plugin implementation definition.
59 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
60 * The date formatter service.
61 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
62 * The request stack used to determine the current time.
64 public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
65 parent::__construct($configuration, $plugin_id, $plugin_definition);
66 $this->dateFormatter = $date_formatter;
67 $this->requestStack = $request_stack;
69 // Date format depends on field storage format.
70 $definition = $this->getFieldStorageDefinition();
71 if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
72 $this->dateFormat = DATETIME_DATE_STORAGE_FORMAT;
79 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
84 $container->get('date.formatter'),
85 $container->get('request_stack')
90 * Override parent method, which deals with dates as integers.
92 protected function opBetween($field) {
93 $origin = ($this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
94 $a = intval(strtotime($this->value['min'], $origin));
95 $b = intval(strtotime($this->value['max'], $origin));
97 // Formatting will vary on date storage.
99 // Convert to ISO format and format for query. UTC timezone is used since
100 // dates are stored in UTC.
101 $a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
102 $b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
104 // This is safe because we are manually scrubbing the values.
105 $operator = strtoupper($this->operator);
106 $field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
107 $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b");
111 * Override parent method, which deals with dates as integers.
113 protected function opSimple($field) {
114 $origin = (!empty($this->value['type']) && $this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
115 $value = intval(strtotime($this->value['value'], $origin));
117 // Convert to ISO. UTC is used since dates are stored in UTC.
118 $value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
120 // This is safe because we are manually scrubbing the value.
121 $field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
122 $this->query->addWhereExpression($this->options['group'], "$field $this->operator $value");