-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_recur.install
327 lines (287 loc) · 12.4 KB
/
date_recur.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* @file
* Install, update and uninstall functions for date_recur module.
*/
declare(strict_types = 1);
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Utility\UpdateException;
use Drupal\date_recur\Entity\DateRecurInterpreter;
use Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Implements hook_requirements().
*/
function date_recur_requirements($phase): array {
$requirements = [];
if (!class_exists('\RRule\RRule')) {
$requirements['date_recur_dependencies'] = [
'title' => t('Date recur'),
'description' => \t('Date recur has unmet Composer dependencies. Read the <a href="@url">documentation</a> on how to install them.', [
'@url' => 'https://www.drupal.org/node/2627292',
]),
'severity' => \REQUIREMENT_ERROR,
];
}
return $requirements;
}
/**
* Update date recur fields with new column schema for 'rrule' property.
*/
function date_recur_update_8201(&$sandbox): void {
// The process is:
// 1. Determine all entity types with date recur fields and load their
// definitions.
// 2. Update the SQL table schema for all relevant tables.
// 3. Inform Drupal of the expected table schemas.
// 4. Update Drupal's entity type definitions.
$fieldType = 'date_recur';
$schema = \Drupal::database()->schema();
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$entityFieldMap = $entityFieldManager->getFieldMapByFieldType($fieldType);
$entityStorageSchemaSql = \Drupal::keyValue('entity.storage_schema.sql');
/** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $lastInstalledSchemaRepository */
$lastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
// The new SQL schema for our column.
$specification = [
'description' => 'The repeat rule.',
'type' => 'text',
];
// Iterate over all date_recur fields for all entity types.
foreach ($entityFieldMap as $entityTypeId => $fields) {
$entityStorage = $entityTypeManager->getStorage($entityTypeId);
if (!$entityStorage instanceof SqlEntityStorageInterface) {
continue;
}
$entityType = $entityTypeManager->getDefinition($entityTypeId);
// Loads definitions for all fields (even non date_recur).
$entityFieldStorageDefinitions = $entityFieldManager->getFieldStorageDefinitions($entityTypeId);
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $tableMapping */
$tableMapping = $entityStorage->getTableMapping($entityFieldStorageDefinitions);
// Intersect date_recur fields with storage definitions for all fields.
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fieldDefinitions */
$fieldDefinitions = array_intersect_key($entityFieldStorageDefinitions, $fields);
// Iterate over all date_recur field definitions for this entity type.
foreach ($fieldDefinitions as $fieldDefinition) {
$fieldName = $fieldDefinition->getName();
$tables = [];
$tables[] = $tableMapping->getFieldTableName($fieldName);
if ($entityType->isRevisionable() && $fieldDefinition->isRevisionable()) {
$tables[] = $tableMapping->getDedicatedRevisionTableName($fieldDefinition);
}
// Field type column names map to real table column names.
$columns = $tableMapping->getColumnNames($fieldName);
$rruleColumnName = $columns['rrule'];
foreach ($tables as $table) {
// Change the column spec here.
$schema->changeField($table, $rruleColumnName, $rruleColumnName, $specification);
}
// Update the tracked entity table schema.
$schemaKey = "$entityTypeId.field_schema_data.$fieldName";
$fieldSchemaData = $entityStorageSchemaSql->get($schemaKey);
foreach ($fieldSchemaData as $tableName => $fieldSchema) {
// Type is now 'text'.
$fieldSchemaData[$tableName]['fields'][$rruleColumnName]['type'] = 'text';
// Type previously was 'varchar', remove the length portion.
unset($fieldSchemaData[$tableName]['fields'][$rruleColumnName]['length']);
}
$entityStorageSchemaSql->set($schemaKey, $fieldSchemaData);
// Update cached entity definitions for entity types with of single
// cardinality base fields.
if ($tableMapping->allowsSharedTableStorage($fieldDefinition)) {
$definitions = $lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entityTypeId);
$definitions[$fieldName] = $fieldDefinition;
$lastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entityTypeId, $definitions);
}
}
}
}
/**
* Add a default value for new 'rrule_max_length' setting on date recur fields.
*
* Applies to attached fields only.
*/
function date_recur_update_8202(&$sandbox): void {
$fieldType = 'date_recur';
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$entityFieldMap = $entityFieldManager->getFieldMapByFieldType($fieldType);
// Iterate over all date_recur fields for all entity types.
foreach ($entityFieldMap as $entityTypeId => $fields) {
$entityStorage = $entityTypeManager->getStorage($entityTypeId);
if (!$entityStorage instanceof SqlEntityStorageInterface) {
continue;
}
// Loads definitions for all fields (even non date_recur).
$entityFieldStorageDefinitions = $entityFieldManager->getFieldStorageDefinitions($entityTypeId);
// Intersect date_recur fields with storage definitions for all fields.
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fieldDefinitions */
$fieldDefinitions = array_intersect_key($entityFieldStorageDefinitions, $fields);
// Iterate over all date_recur field definitions for this entity type.
foreach ($fieldDefinitions as $fieldDefinition) {
if ($fieldDefinition instanceof FieldStorageConfig) {
// Ignore base fields, etc.
$fieldDefinition->setSetting('rrule_max_length', 256);
$fieldDefinition->save();
}
}
}
}
/**
* Checks for invalid time zones in storage.
*/
function date_recur_update_8203(&$sandbox): void {
$timeZoneList = timezone_identifiers_list();
$database = \Drupal::database();
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$entityFieldMap = $entityFieldManager->getFieldMapByFieldType('date_recur');
// Iterate over all date_recur fields for all entity types.
foreach ($entityFieldMap as $entityTypeId => $fields) {
$entityStorage = $entityTypeManager->getStorage($entityTypeId);
if (!$entityStorage instanceof SqlEntityStorageInterface) {
continue;
}
// Loads definitions for all fields (even non date_recur).
$entityFieldStorageDefinitions = $entityFieldManager->getFieldStorageDefinitions($entityTypeId);
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $tableMapping */
$tableMapping = $entityStorage->getTableMapping($entityFieldStorageDefinitions);
// Intersect date_recur fields with storage definitions for all fields.
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fieldDefinitions */
$fieldDefinitions = array_intersect_key($entityFieldStorageDefinitions, $fields);
// Iterate over all date_recur field definitions for this entity type.
foreach ($fieldDefinitions as $fieldDefinition) {
$fieldName = $fieldDefinition->getName();
$table = $tableMapping->getFieldTableName($fieldName);
// Field type column names map to real table column names.
$columns = $tableMapping->getColumnNames($fieldName);
$timeZoneColumnName = $columns['timezone'];
$select = $database->select($table, 'field_table');
$select->addField('field_table', $timeZoneColumnName);
$select->distinct();
$result = $select->execute();
if (!$result) {
throw new \Exception('Problem executing query.');
}
$allTimeZones = $result->fetchCol($timeZoneColumnName);
$diff = array_diff($allTimeZones, $timeZoneList);
if (count($diff)) {
// This exception prevents further updates from happening until it is
// resolved.
throw new UpdateException(sprintf('Invalid time zones found for field `%s` in table `%s`: `%s`. Please resolve these invalid values manually before continuing.', $fieldName, $table, implode(', ', $diff)));
}
}
}
}
/**
* Update view and form display configurations.
*/
function date_recur_update_8204(&$sandbox): void {
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface[] $displays */
$displays = EntityFormDisplay::loadMultiple();
foreach ($displays as $display) {
$components = $display->getComponents();
foreach ($components as $component => $options) {
$updated = FALSE;
$type = $options['type'] ?? NULL;
if ($type === 'date_recur_default_widget') {
// Change the ID.
$options['type'] = 'date_recur_basic_widget';
// No settings to update.
$updated = TRUE;
}
// Interactive widget did not change ID or settings.
if ($updated) {
$display->setComponent($component, $options);
}
}
$display->save();
}
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] $displays */
$displays = EntityViewDisplay::loadMultiple();
foreach ($displays as $display) {
$components = $display->getComponents();
foreach ($components as $component => $options) {
$updated = FALSE;
$type = $options['type'] ?? NULL;
if ($type === 'date_recur_default_formatter') {
// Option: show_next. Unchanged.
// Option: count_per_item. Unchanged.
// Option: occurrence_format_type. Unchanged.
// Option: same_end_date_format_type. Unchanged.
// Change the ID.
$options['type'] = 'date_recur_basic_formatter';
// Option: show_rrule. Changed to 'interpreter'.
if (isset($options['show_rrule'])) {
unset($options['show_rrule']);
}
$updated = TRUE;
}
if ($updated) {
$display->setComponent($component, $options);
}
}
$display->save();
}
}
/**
* Add a default interpreter.
*/
function date_recur_update_8205(&$sandbox): void {
if (DateFormat::load('long') && !DateRecurInterpreter::load('default_interpreter')) {
$configFactory = \Drupal::configFactory();
$interpreter = $configFactory->getEditable('date_recur.interpreter.default_interpreter');
$interpreter->setData([
'id' => 'default_interpreter',
'label' => 'Default interpreter',
'plugin' => 'rl',
'settings' => [
'show_start_date' => TRUE,
'show_until' => TRUE,
'date_format' => 'long',
'show_infinite' => TRUE,
],
]);
$interpreter->save(TRUE);
}
}
/**
* Removes default time zone setting from widgets extending basic widget.
*/
function date_recur_update_8206(&$sandbox): void {
// Get widgets implementing the same class as basic widget.
/** @var \Drupal\Core\Field\WidgetPluginManager $fieldWidgetPluginManager */
$basicWidgetClass = DateRecurBasicWidget::class;
$basicWidgetDerivativeWidgetIds = [];
$basicWidgetDerivativeWidgetIds[] = 'date_recur_basic_widget';
$fieldWidgetPluginManager = \Drupal::service('plugin.manager.field.widget');
foreach ($fieldWidgetPluginManager->getDefinitions() as $widgetId => $definition) {
$class = $definition['class'];
if (class_exists($class) && (new \ReflectionClass($class))->isSubclassOf($basicWidgetClass)) {
$basicWidgetDerivativeWidgetIds[] = $widgetId;
}
}
// Find form displays using any of the widgets extending basic widget.
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface[] $displays */
$displays = EntityFormDisplay::loadMultiple();
foreach ($displays as $display) {
$updated = 0;
$components = $display->getComponents();
foreach ($components as $component => $options) {
$widgetId = $options['type'] ?? NULL;
if (in_array($widgetId, $basicWidgetDerivativeWidgetIds)) {
// Unset the old setting.
unset($options['settings']['timezone_override']);
$updated++;
$display->setComponent($component, $options);
}
}
if ($updated > 0) {
$display->save();
}
}
}