Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Field hooks to implement a datetime field that stores a start and end date.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@17
|
10 use Drupal\views\Views;
|
Chris@17
|
11 use Drupal\views\ViewEntityInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Implements hook_help().
|
Chris@0
|
15 */
|
Chris@0
|
16 function datetime_range_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
17 switch ($route_name) {
|
Chris@0
|
18 case 'help.page.datetime_range':
|
Chris@0
|
19 $output = '';
|
Chris@0
|
20 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@18
|
21 $output .= '<p>' . t('The Datetime Range module provides a Date field that stores start dates and times, as well as end dates and times. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI module help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":datetime_do">online documentation for the Datetime Range module</a>.', [':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#', ':datetime_do' => 'https://www.drupal.org/documentation/modules/datetime_range']) . '</p>';
|
Chris@0
|
22 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
23 $output .= '<dl>';
|
Chris@0
|
24 $output .= '<dt>' . t('Managing and displaying date fields') . '</dt>';
|
Chris@18
|
25 $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the Date field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
|
Chris@0
|
26 $output .= '<dt>' . t('Displaying dates') . '</dt>';
|
Chris@18
|
27 $output .= '<dd>' . t('Dates can be displayed using the <em>Plain</em> or the <em>Default</em> formatter. The <em>Plain</em> formatter displays the date in the <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. If you choose the <em>Default</em> formatter, you can choose a format from a predefined list that can be managed on the <a href=":date_format_list">Date and time formats</a> page.', [':date_format_list' => Url::fromRoute('entity.date_format.collection')->toString()]) . '</dd>';
|
Chris@0
|
28 $output .= '</dl>';
|
Chris@0
|
29 return $output;
|
Chris@0
|
30 }
|
Chris@0
|
31 }
|
Chris@17
|
32
|
Chris@17
|
33 /**
|
Chris@18
|
34 * Implements hook_ENTITY_TYPE_presave().
|
Chris@17
|
35 *
|
Chris@17
|
36 * When a view is saved using the old string or standard plugin format for
|
Chris@17
|
37 * Datetime Range filters or sorts, they will automatically be updated to
|
Chris@17
|
38 * Datetime filters or sorts. Old plugins usage must to be considered
|
Chris@17
|
39 * deprecated and must be converted before 9.0.0, when this updating layer will
|
Chris@17
|
40 * be removed.
|
Chris@17
|
41 *
|
Chris@17
|
42 * @deprecated in Drupal 8.5.x and will be removed before 9.0.0.
|
Chris@17
|
43 *
|
Chris@17
|
44 * @see https://www.drupal.org/node/2857691
|
Chris@17
|
45 */
|
Chris@17
|
46 function datetime_range_view_presave(ViewEntityInterface $view) {
|
Chris@17
|
47 $config_factory = \Drupal::configFactory();
|
Chris@17
|
48 $displays = $view->get('display');
|
Chris@17
|
49 $changed = FALSE;
|
Chris@17
|
50
|
Chris@17
|
51 foreach ($displays as $display_name => &$display) {
|
Chris@17
|
52
|
Chris@17
|
53 // Update datetime_range filters.
|
Chris@17
|
54 if (isset($display['display_options']['filters'])) {
|
Chris@17
|
55 foreach ($display['display_options']['filters'] as $field_name => &$filter) {
|
Chris@17
|
56 if ($filter['plugin_id'] === 'string') {
|
Chris@17
|
57 $table_data = Views::viewsData()->get($filter['table']);
|
Chris@17
|
58 if (!$table_data) {
|
Chris@17
|
59 continue;
|
Chris@17
|
60 }
|
Chris@17
|
61
|
Chris@17
|
62 // Get field config.
|
Chris@17
|
63 $filter_views_data = $table_data[$filter['field']]['filter'];
|
Chris@17
|
64 if (!isset($filter_views_data['entity_type']) || !isset($filter_views_data['field_name'])) {
|
Chris@17
|
65 continue;
|
Chris@17
|
66 }
|
Chris@17
|
67 $field_storage_name = 'field.storage.' . $filter_views_data['entity_type'] . '.' . $filter_views_data['field_name'];
|
Chris@17
|
68 $field_configuration = $config_factory->get($field_storage_name);
|
Chris@17
|
69
|
Chris@17
|
70 if ($field_configuration->get('type') === 'daterange') {
|
Chris@17
|
71
|
Chris@17
|
72 // Set entity_type if missing.
|
Chris@17
|
73 if (!isset($filter['entity_type'])) {
|
Chris@17
|
74 $filter['entity_type'] = $filter_views_data['entity_type'];
|
Chris@17
|
75 }
|
Chris@17
|
76
|
Chris@17
|
77 // Set datetime plugin_id.
|
Chris@17
|
78 $filter['plugin_id'] = 'datetime';
|
Chris@17
|
79
|
Chris@17
|
80 // Create datetime value array.
|
Chris@17
|
81 $datetime_value = [
|
Chris@17
|
82 'min' => '',
|
Chris@17
|
83 'max' => '',
|
Chris@17
|
84 'value' => $filter['value'],
|
Chris@17
|
85 'type' => 'date',
|
Chris@17
|
86 ];
|
Chris@17
|
87
|
Chris@17
|
88 // Map string operator/value to numeric equivalent.
|
Chris@17
|
89 switch ($filter['operator']) {
|
Chris@17
|
90 case '=':
|
Chris@17
|
91 case 'empty':
|
Chris@17
|
92 case 'not empty':
|
Chris@17
|
93 $operator = $filter['operator'];
|
Chris@17
|
94 break;
|
Chris@17
|
95
|
Chris@17
|
96 case '!=':
|
Chris@17
|
97 case 'not':
|
Chris@17
|
98 $operator = '!=';
|
Chris@17
|
99 break;
|
Chris@17
|
100
|
Chris@17
|
101 case 'starts':
|
Chris@17
|
102 $operator = 'regular_expression';
|
Chris@17
|
103 $datetime_value['value'] = '^' . preg_quote($datetime_value['value']);
|
Chris@17
|
104 break;
|
Chris@17
|
105
|
Chris@17
|
106 case 'ends':
|
Chris@17
|
107 $operator = 'regular_expression';
|
Chris@17
|
108 $datetime_value['value'] = preg_quote($datetime_value['value']) . '$';
|
Chris@17
|
109 break;
|
Chris@17
|
110
|
Chris@17
|
111 default:
|
Chris@17
|
112 $operator = 'regular_expression';
|
Chris@17
|
113 // Add .* to prevent blank regexes.
|
Chris@17
|
114 if (empty($datetime_value['value'])) {
|
Chris@17
|
115 $datetime_value['value'] = '.*';
|
Chris@17
|
116 }
|
Chris@17
|
117 else {
|
Chris@17
|
118 $datetime_value['value'] = preg_quote($datetime_value['value']);
|
Chris@17
|
119 }
|
Chris@17
|
120 }
|
Chris@17
|
121
|
Chris@17
|
122 // Set value and operator.
|
Chris@17
|
123 $filter['value'] = $datetime_value;
|
Chris@17
|
124 $filter['operator'] = $operator;
|
Chris@17
|
125 $changed = TRUE;
|
Chris@17
|
126 @trigger_error('Use of string filters for datetime_range fields is deprecated. Use the datetime filters instead. See https://www.drupal.org/node/2857691', E_USER_DEPRECATED);
|
Chris@17
|
127 }
|
Chris@17
|
128 }
|
Chris@17
|
129 }
|
Chris@17
|
130 }
|
Chris@17
|
131
|
Chris@17
|
132 // Update datetime_range sort handlers.
|
Chris@17
|
133 if (isset($display['display_options']['sorts'])) {
|
Chris@17
|
134 foreach ($display['display_options']['sorts'] as $field_name => &$sort) {
|
Chris@17
|
135 if ($sort['plugin_id'] === 'standard') {
|
Chris@17
|
136 $table_data = Views::viewsData()->get($sort['table']);
|
Chris@17
|
137 if (!$table_data) {
|
Chris@17
|
138 continue;
|
Chris@17
|
139 }
|
Chris@17
|
140
|
Chris@17
|
141 // Get field config.
|
Chris@17
|
142 $sort_views_data = $table_data[$sort['field']]['sort'];
|
Chris@17
|
143 if (!isset($sort_views_data['entity_type']) || !isset($sort_views_data['field_name'])) {
|
Chris@17
|
144 continue;
|
Chris@17
|
145 }
|
Chris@17
|
146 $field_storage_name = 'field.storage.' . $sort_views_data['entity_type'] . '.' . $sort_views_data['field_name'];
|
Chris@17
|
147 $field_configuration = $config_factory->get($field_storage_name);
|
Chris@17
|
148
|
Chris@17
|
149 if ($field_configuration->get('type') === 'daterange') {
|
Chris@17
|
150
|
Chris@17
|
151 // Set entity_type if missing.
|
Chris@17
|
152 if (!isset($sort['entity_type'])) {
|
Chris@17
|
153 $sort['entity_type'] = $sort_views_data['entity_type'];
|
Chris@17
|
154 }
|
Chris@17
|
155
|
Chris@17
|
156 // Set datetime plugin_id.
|
Chris@17
|
157 $sort['plugin_id'] = 'datetime';
|
Chris@17
|
158 $changed = TRUE;
|
Chris@17
|
159 @trigger_error('Use of standard sort handlers for datetime_range fields is deprecated. Use the datetime sort handlers instead. See https://www.drupal.org/node/2857691', E_USER_DEPRECATED);
|
Chris@17
|
160 }
|
Chris@17
|
161 }
|
Chris@17
|
162 }
|
Chris@17
|
163 }
|
Chris@17
|
164 }
|
Chris@17
|
165
|
Chris@17
|
166 if ($changed) {
|
Chris@17
|
167 $view->set('display', $displays);
|
Chris@17
|
168 }
|
Chris@17
|
169 }
|