1<?php
2
3namespace Drupal\datetime\Plugin\views\argument;
4
5use Drupal\Core\Datetime\DateFormatterInterface;
6use Drupal\Core\Routing\RouteMatchInterface;
7use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
8use Drupal\views\FieldAPIHandlerTrait;
9use Drupal\views\Plugin\views\argument\Date as NumericDate;
10
11/**
12 * Abstract argument handler for dates.
13 *
14 * Adds an option to set a default argument based on the current date.
15 *
16 * Definitions terms:
17 * - many to one: If true, the "many to one" helper will be used.
18 * - invalid input: A string to give to the user for obviously invalid input.
19 *                  This is deprecated in favor of argument validators.
20 *
21 * @see \Drupal\views\ManyTonOneHelper
22 *
23 * @ingroup views_argument_handlers
24 *
25 * @ViewsArgument("datetime")
26 */
27class Date extends NumericDate {
28
29  use FieldAPIHandlerTrait;
30
31  /**
32   * Determines if the timezone offset is calculated.
33   *
34   * @var bool
35   */
36  protected $calculateOffset = TRUE;
37
38  /**
39   * {@inheritdoc}
40   */
41  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, DateFormatterInterface $date_formatter) {
42    parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match, $date_formatter);
43
44    $definition = $this->getFieldStorageDefinition();
45    if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
46      // Timezone offset calculation is not applicable to dates that are stored
47      // as date-only.
48      $this->calculateOffset = FALSE;
49    }
50  }
51
52  /**
53   * {@inheritdoc}
54   */
55  public function getDateField() {
56    // Use string date storage/formatting since datetime fields are stored as
57    // strings rather than UNIX timestamps.
58    return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
59  }
60
61  /**
62   * {@inheritdoc}
63   */
64  public function getDateFormat($format) {
65    // Pass in the string-field option.
66    return $this->query->getDateFormat($this->getDateField(), $format, TRUE);
67  }
68
69}
70