You are here

class Date in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Render/Element/Date.php \Drupal\Core\Render\Element\Date
  2. 10 core/modules/views/src/Plugin/views/filter/Date.php \Drupal\views\Plugin\views\filter\Date
  3. 10 core/modules/views/src/Plugin/views/sort/Date.php \Drupal\views\Plugin\views\sort\Date
  4. 10 core/modules/views/src/Plugin/views/argument/Date.php \Drupal\views\Plugin\views\argument\Date
  5. 10 core/modules/views/src/Plugin/views/field/Date.php \Drupal\views\Plugin\views\field\Date
  6. 10 core/modules/datetime/src/Plugin/views/filter/Date.php \Drupal\datetime\Plugin\views\filter\Date
  7. 10 core/modules/datetime/src/Plugin/views/sort/Date.php \Drupal\datetime\Plugin\views\sort\Date
  8. 10 core/modules/datetime/src/Plugin/views/argument/Date.php \Drupal\datetime\Plugin\views\argument\Date
Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/argument/Date.php \Drupal\views\Plugin\views\argument\Date
  2. 9 core/modules/views/src/Plugin/views/argument/Date.php \Drupal\views\Plugin\views\argument\Date

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.

Plugin annotation

@ViewsArgument("date");

Hierarchy

Expanded class hierarchy of Date

See also

\Drupal\views\ManyToOneHelper

1 file declares its use of Date
Date.php in core/modules/datetime/src/Plugin/views/argument/Date.php
23 string references to 'Date'
Date::getSortName in core/modules/views/src/Plugin/views/argument/Date.php
Datetime::processDatetime in core/lib/Drupal/Core/Datetime/Element/Datetime.php
Expands a datetime element type into date and/or time elements.
DateTimeSchemaTest::testDateTimeSchema in core/modules/datetime/tests/src/Kernel/Views/DateTimeSchemaTest.php
Tests argument plugin schema.
DbLogController::eventDetails in core/modules/dblog/src/Controller/DbLogController.php
Displays details about a specific database log message.
DbLogController::overview in core/modules/dblog/src/Controller/DbLogController.php
Displays a listing of database log messages.

... See full list

File

core/modules/views/src/Plugin/views/argument/Date.php, line 28

Namespace

Drupal\views\Plugin\views\argument
View source
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';
  public $option_name = 'default_argument_date';

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * 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.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, DateFormatterInterface $date_formatter) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->routeMatch = $route_match;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@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'), $container
      ->get('date.formatter'));
  }

  /**
   * 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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Date::$argFormat protected property The date format used in the query. 12
Date::$dateFormatter protected property The date formatter service.
Date::$format protected property The date format used in the title. 4
Date::$option_name public property
Date::$routeMatch protected property The route match.
Date::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Date::defaultArgumentForm public function Add an option to set the default value to the current date.
Date::getDefaultArgument public function Set the empty argument value to the current date, formatted appropriately for this argument.
Date::getFormula public function Overrides Formula::getFormula
Date::getSortName public function
Date::__construct public function Constructs a new Date instance. 1
Formula::$formula public property
Formula::init public function
Formula::query public function Build the query based upon the formula.
Formula::summaryQuery protected function Build the summary query based on a formula.