Newer
Older
* Contains \Drupal\views\Plugin\views\argument\Date.
namespace Drupal\views\Plugin\views\argument;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
catch
committed
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Abstract argument handler for dates.
*
* Adds an option to set a default argument based on the current date.
*
* Definitions terms:
* - many to one: If true, the "many to one" helper will be used.
* - invalid input: A string to give to the user for obviously invalid input.
* This is deprecated in favor of argument validators.
*
Alex Pott
committed
* @see \Drupal\views\ManyTonOneHelper
* @ViewsArgument("date")
class Date extends Formula implements ContainerFactoryPluginInterface {
/**
* The date format used in the title.
*
* @var string
*/
protected $format;
/**
* The date format used in the query.
*
* @var string
*/
protected $argFormat = 'Y-m-d';
var $option_name = 'default_argument_date';
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a new Date instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->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.
*/
Dries Buytaert
committed
public function defaultArgumentForm(&$form, FormStateInterface $form_state) {
Alex Pott
committed
parent::defaultArgumentForm($form, $form_state);
catch
committed
$form['default_argument_type']['#options'] += array('date' => $this->t('Current date'));
$form['default_argument_type']['#options'] += array('node_created' => $this->t("Current node's creation time"));
$form['default_argument_type']['#options'] += array('node_changed' => $this->t("Current node's update time"));
}
/**
* Set the empty argument value to the current date,
* formatted appropriately for this argument.
*/
Alex Pott
committed
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'], array('node_created', 'node_changed'))) {
$node = $this->routeMatch->getParameter('node');
catch
committed
if (!($node instanceof NodeInterface)) {
Alex Pott
committed
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());
Alex Pott
committed
return parent::getDefaultArgument($raw);
/**
* {@inheritdoc}
*/
public function getSortName() {
catch
committed
return $this->t('Date', array(), array('context' => 'Sort order'));
Alex Pott
committed
* Overrides \Drupal\views\Plugin\views\argument\Formula::getFormula().
Alex Pott
committed
public function getFormula() {
$this->formula = $this->getDateFormat($this->argFormat);
Alex Pott
committed
return parent::getFormula();