3 namespace Drupal\views\Plugin\views\argument;
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;
12 * Argument handler for dates.
14 * Adds an option to set a default argument based on the current date.
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.
21 * @see \Drupal\views\ManyTonOneHelper
23 * @ingroup views_argument_handlers
25 * @ViewsArgument("date")
27 class Date extends Formula implements ContainerFactoryPluginInterface {
30 * The date format used in the title.
37 * The date format used in the query.
41 protected $argFormat = 'Y-m-d';
43 public $option_name = 'default_argument_date';
48 * @var \Drupal\Core\Routing\RouteMatchInterface
50 protected $routeMatch;
53 * Constructs a new Date instance.
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.
62 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
65 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
66 parent::__construct($configuration, $plugin_id, $plugin_definition);
68 $this->routeMatch = $route_match;
74 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
79 $container->get('current_route_match')
84 * Add an option to set the default value to the current date.
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")];
94 * Set the empty argument value to the current date,
95 * formatted appropriately for this argument.
97 public function getDefaultArgument($raw = FALSE) {
98 if (!$raw && $this->options['default_argument_type'] == 'date') {
99 return date($this->argFormat, REQUEST_TIME);
101 elseif (!$raw && in_array($this->options['default_argument_type'], ['node_created', 'node_changed'])) {
102 $node = $this->routeMatch->getParameter('node');
104 if (!($node instanceof NodeInterface)) {
105 return parent::getDefaultArgument();
107 elseif ($this->options['default_argument_type'] == 'node_created') {
108 return date($this->argFormat, $node->getCreatedTime());
110 elseif ($this->options['default_argument_type'] == 'node_changed') {
111 return date($this->argFormat, $node->getChangedTime());
115 return parent::getDefaultArgument($raw);
121 public function getSortName() {
122 return $this->t('Date', [], ['context' => 'Sort order']);
128 public function getFormula() {
129 $this->formula = $this->getDateFormat($this->argFormat);
130 return parent::getFormula();