1512dcce4928e02d861c4f4d4ac35f7f2512a67a
[yaffs-website] / web / core / modules / datetime / datetime.views.inc
1 <?php
2
3 /**
4  * @file
5  * Provides views data for the datetime module.
6  */
7
8 use Drupal\field\FieldStorageConfigInterface;
9
10 /**
11  * Implements hook_field_views_data().
12  */
13 function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
14   return datetime_type_field_views_data_helper($field_storage, [], $field_storage->getMainPropertyName());
15 }
16
17 /**
18  * Provides Views integration for any datetime-based fields.
19  *
20  * Overrides the default Views data for datetime-based fields, adding datetime
21  * views plugins. Modules defining new datetime-based fields may use this
22  * function to simplify Views integration.
23  *
24  * @param \Drupal\field\FieldStorageConfigInterface $field_storage
25  *   The field storage config entity.
26  * @param array $data
27  *   Field view data or views_field_default_views_data($field_storage) if empty.
28  * @param string $column_name
29  *   The schema column name with the datetime value.
30  *
31  * @return array
32  *   The array of field views data with the datetime plugin.
33  *
34  * @see datetime_field_views_data()
35  * @see datetime_range_field_views_data()
36  */
37 function datetime_type_field_views_data_helper(FieldStorageConfigInterface $field_storage, array $data, $column_name) {
38   // @todo This code only covers configurable fields, handle base table fields
39   //   in https://www.drupal.org/node/2489476.
40   $data = empty($data) ? views_field_default_views_data($field_storage) : $data;
41   foreach ($data as $table_name => $table_data) {
42     // Set the 'datetime' filter type.
43     $data[$table_name][$field_storage->getName() . '_' . $column_name]['filter']['id'] = 'datetime';
44
45     // Set the 'datetime' argument type.
46     $data[$table_name][$field_storage->getName() . '_' . $column_name]['argument']['id'] = 'datetime';
47
48     // Create year, month, and day arguments.
49     $group = $data[$table_name][$field_storage->getName() . '_' . $column_name]['group'];
50     $arguments = [
51       // Argument type => help text.
52       'year' => t('Date in the form of YYYY.'),
53       'month' => t('Date in the form of MM (01 - 12).'),
54       'day' => t('Date in the form of DD (01 - 31).'),
55       'week' => t('Date in the form of WW (01 - 53).'),
56       'year_month' => t('Date in the form of YYYYMM.'),
57       'full_date' => t('Date in the form of CCYYMMDD.'),
58     ];
59     foreach ($arguments as $argument_type => $help_text) {
60       $column_name_text = $column_name === $field_storage->getMainPropertyName() ? '' : ':' . $column_name;
61       $data[$table_name][$field_storage->getName() . '_' . $column_name . '_' . $argument_type] = [
62         'title' => t('@label@column (@argument)', [
63           '@label' => $field_storage->getLabel(),
64           '@column' => $column_name_text,
65           '@argument' => $argument_type,
66         ]),
67         'help' => $help_text,
68         'argument' => [
69           'field' => $field_storage->getName() . '_' . $column_name,
70           'id' => 'datetime_' . $argument_type,
71           'entity_type' => $field_storage->getTargetEntityTypeId(),
72           'field_name' => $field_storage->getName(),
73         ],
74         'group' => $group,
75       ];
76     }
77
78     // Set the 'datetime' sort handler.
79     $data[$table_name][$field_storage->getName() . '_' . $column_name]['sort']['id'] = 'datetime';
80   }
81
82   return $data;
83 }