Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / sort / Date.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\sort;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Basic sort handler for dates.
9  *
10  * This handler enables granularity, which is the ability to make dates
11  * equivalent based upon nearness.
12  *
13  * @ViewsSort("date")
14  */
15 class Date extends SortPluginBase {
16
17   protected function defineOptions() {
18     $options = parent::defineOptions();
19
20     $options['granularity'] = ['default' => 'second'];
21
22     return $options;
23   }
24
25   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
26     parent::buildOptionsForm($form, $form_state);
27
28     $form['granularity'] = [
29       '#type' => 'radios',
30       '#title' => $this->t('Granularity'),
31       '#options' => [
32         'second' => $this->t('Second'),
33         'minute' => $this->t('Minute'),
34         'hour'   => $this->t('Hour'),
35         'day'    => $this->t('Day'),
36         'month'  => $this->t('Month'),
37         'year'   => $this->t('Year'),
38       ],
39       '#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.'),
40       '#default_value' => $this->options['granularity'],
41     ];
42   }
43
44   /**
45    * Called to add the sort to a query.
46    */
47   public function query() {
48     $this->ensureMyTable();
49     switch ($this->options['granularity']) {
50       case 'second':
51       default:
52         $this->query->addOrderBy($this->tableAlias, $this->realField, $this->options['order']);
53         return;
54       case 'minute':
55         $formula = $this->getDateFormat('YmdHi');
56         break;
57       case 'hour':
58         $formula = $this->getDateFormat('YmdH');
59         break;
60       case 'day':
61         $formula = $this->getDateFormat('Ymd');
62         break;
63       case 'month':
64         $formula = $this->getDateFormat('Ym');
65         break;
66       case 'year':
67         $formula = $this->getDateFormat('Y');
68         break;
69     }
70
71     // Add the field.
72     $this->query->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_' . $this->options['granularity']);
73   }
74
75 }