routeMatch = $route_match; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('current_route_match') ); } /** * Add an option to set the default value to the current date. */ public function defaultArgumentForm(&$form, FormStateInterface $form_state) { parent::defaultArgumentForm($form, $form_state); $form['default_argument_type']['#options'] += ['date' => $this->t('Current date')]; $form['default_argument_type']['#options'] += ['node_created' => $this->t("Current node's creation time")]; $form['default_argument_type']['#options'] += ['node_changed' => $this->t("Current node's update time")]; } /** * Set the empty argument value to the current date, * formatted appropriately for this argument. */ public function getDefaultArgument($raw = FALSE) { if (!$raw && $this->options['default_argument_type'] == 'date') { return date($this->argFormat, REQUEST_TIME); } elseif (!$raw && in_array($this->options['default_argument_type'], ['node_created', 'node_changed'])) { $node = $this->routeMatch->getParameter('node'); if (!($node instanceof NodeInterface)) { return parent::getDefaultArgument(); } elseif ($this->options['default_argument_type'] == 'node_created') { return date($this->argFormat, $node->getCreatedTime()); } elseif ($this->options['default_argument_type'] == 'node_changed') { return date($this->argFormat, $node->getChangedTime()); } } return parent::getDefaultArgument($raw); } /** * {@inheritdoc} */ public function getSortName() { return $this->t('Date', [], ['context' => 'Sort order']); } /** * {@inheritdoc} */ public function getFormula() { $this->formula = $this->getDateFormat($this->argFormat); return parent::getFormula(); } }