Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / argument / Date.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\argument;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\node\NodeInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * 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("date")
26  */
27 class Date extends Formula implements ContainerFactoryPluginInterface {
28
29   /**
30    * The date format used in the title.
31    *
32    * @var string
33    */
34   protected $format;
35
36   /**
37    * The date format used in the query.
38    *
39    * @var string
40    */
41   protected $argFormat = 'Y-m-d';
42
43   public $option_name = 'default_argument_date';
44
45   /**
46    * The route match.
47    *
48    * @var \Drupal\Core\Routing\RouteMatchInterface
49    */
50   protected $routeMatch;
51
52   /**
53    * Constructs a new Date instance.
54    *
55    * @param array $configuration
56    *   A configuration array containing information about the plugin instance.
57    * @param string $plugin_id
58    *   The plugin_id for the plugin instance.
59    * @param mixed $plugin_definition
60    *   The plugin implementation definition.
61    *
62    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
63    *   The route match.
64    */
65   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
66     parent::__construct($configuration, $plugin_id, $plugin_definition);
67
68     $this->routeMatch = $route_match;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
75     return new static(
76       $configuration,
77       $plugin_id,
78       $plugin_definition,
79       $container->get('current_route_match')
80     );
81   }
82
83   /**
84    * Add an option to set the default value to the current date.
85    */
86   public function defaultArgumentForm(&$form, FormStateInterface $form_state) {
87     parent::defaultArgumentForm($form, $form_state);
88     $form['default_argument_type']['#options'] += ['date' => $this->t('Current date')];
89     $form['default_argument_type']['#options'] += ['node_created' => $this->t("Current node's creation time")];
90     $form['default_argument_type']['#options'] += ['node_changed' => $this->t("Current node's update time")];
91   }
92
93   /**
94    * Set the empty argument value to the current date,
95    * formatted appropriately for this argument.
96    */
97   public function getDefaultArgument($raw = FALSE) {
98     if (!$raw && $this->options['default_argument_type'] == 'date') {
99       return date($this->argFormat, REQUEST_TIME);
100     }
101     elseif (!$raw && in_array($this->options['default_argument_type'], ['node_created', 'node_changed'])) {
102       $node = $this->routeMatch->getParameter('node');
103
104       if (!($node instanceof NodeInterface)) {
105         return parent::getDefaultArgument();
106       }
107       elseif ($this->options['default_argument_type'] == 'node_created') {
108         return date($this->argFormat, $node->getCreatedTime());
109       }
110       elseif ($this->options['default_argument_type'] == 'node_changed') {
111         return date($this->argFormat, $node->getChangedTime());
112       }
113     }
114
115     return parent::getDefaultArgument($raw);
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getSortName() {
122     return $this->t('Date', [], ['context' => 'Sort order']);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function getFormula() {
129     $this->formula = $this->getDateFormat($this->argFormat);
130     return parent::getFormula();
131   }
132
133 }