class Date

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

Basic sort handler for dates.

This handler enables granularity, which is the ability to make dates equivalent based upon nearness.

Plugin annotation

@ViewsSort("date");

Hierarchy

Expanded class hierarchy of Date

2 files declare their use of Date
Date.php in core/modules/datetime/src/Plugin/views/sort/Date.php
StatisticsLastUpdated.php in core/modules/comment/src/Plugin/views/sort/StatisticsLastUpdated.php
168 string references to 'Date'
Callbacks::dateCallback in core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php
Ajax callback triggered by date.
CallbackTest::providerCallbackArray in core/modules/migrate/tests/src/Unit/process/CallbackTest.php
Data provider for ::testCallbackArray().
claro_preprocess_input in core/themes/claro/claro.theme
Implements template_preprocess_HOOK() for input.
CommentAccessControlHandler::checkFieldAccess in core/modules/comment/src/CommentAccessControlHandler.php
CommentForm::buildEntity in core/modules/comment/src/CommentForm.php
Builds an updated entity object based upon the submitted form values.

... See full list

File

core/modules/views/src/Plugin/views/sort/Date.php, line 15

Namespace

Drupal\views\Plugin\views\sort
View source
class Date extends SortPluginBase {
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['granularity'] = [
      'default' => 'second',
    ];
    return $options;
  }
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['granularity'] = [
      '#type' => 'radios',
      '#title' => $this->t('Granularity'),
      '#options' => [
        'second' => $this->t('Second'),
        'minute' => $this->t('Minute'),
        'hour' => $this->t('Hour'),
        'day' => $this->t('Day'),
        'month' => $this->t('Month'),
        'year' => $this->t('Year'),
      ],
      '#description' => $this->t('The granularity is the smallest unit to use when determining whether two dates are the same; for example, if the granularity is "Year" then all dates in 1999, regardless of when they fall in 1999, will be considered the same date.'),
      '#default_value' => $this->options['granularity'],
    ];
  }
  
  /**
   * Called to add the sort to a query.
   */
  public function query() {
    $this->ensureMyTable();
    switch ($this->options['granularity']) {
      case 'second':
      default:
        $this->query
          ->addOrderBy($this->tableAlias, $this->realField, $this->options['order']);
        return;
      case 'minute':
        $formula = $this->getDateFormat('YmdHi');
        break;

      case 'hour':
        $formula = $this->getDateFormat('YmdH');
        break;

      case 'day':
        $formula = $this->getDateFormat('Ymd');
        break;

      case 'month':
        $formula = $this->getDateFormat('Ym');
        break;

      case 'year':
        $formula = $this->getDateFormat('Y');
        break;

    }
    // Add the field.
    $this->query
      ->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_' . $this->options['granularity']);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.