-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathopeny_node_alert.install
497 lines (454 loc) · 16.3 KB
/
openy_node_alert.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* @file
* Open Y Node Alert install file.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
use \Drupal\user\Entity\Role;
use \Drupal\user\RoleInterface;
/**
* Implements hook_install().
*/
function openy_node_alert_install() {
$roles = Role::loadMultiple([
RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID,
]);
$permissions = [
"restful get alerts_rest_resource",
];
global $_openy_config_import_event;
$_openy_config_import_event = TRUE;
foreach ($roles as $role) {
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->save();
}
$_openy_config_import_event = FALSE;
}
/**
* Implements hook_update_dependencies().
*/
function openy_node_alert_update_dependencies() {
$dependencies['openy_node_blog'] = [
8012 => [
'openy_node_alert' => 8010,
],
];
$dependencies['openy'] = [
8048 => [
'openy_node_alert' => 8010,
],
];
return $dependencies;
}
/**
* Update fixes for the OpenY upgrade path support.
*/
function openy_node_alert_update_fixes() {
// TODO: move this to highler level.
\Drupal::service('module_installer')->install(['openy']);
}
/**
* Update Alert for rabbit hole, hiding pages from anonymous users.
*/
function openy_node_alert_update_8001() {
// This is outdated update and exist in next hooks, no need to import config twice.
}
/**
* Import scheduler config.
*/
function openy_node_alert_update_8002() {
// This is outdated update and exist in next hooks, no need to import config twice.
}
/**
* Update description for fields.
*/
function openy_node_alert_update_8003() {
// This is outdated update and exist in next hooks, no need to import config twice.
}
/**
* Update Open Y alert node feature configs.
*/
function openy_node_alert_update_8004() {
// This is outdated update and exist in next hooks, no need to import config twice.
}
/**
* Update feature configs for Drupal Core upgrade.
*/
function openy_node_alert_update_8005() {
// This is outdated update and exist in next hooks, no need to import config twice.
}
/**
* Update feature configs for Drupal Alert CT upgrade.
*/
function openy_node_alert_update_8006() {
// This is outdated update and exist in next hooks, no need to import config twice.
}
/**
* Rename alert blocks configs.
*/
function openy_node_alert_update_8007() {
\Drupal::service('module_installer')->install(['rest', 'hal', 'serialization']);
openy_node_alert_update_8009();
$configs = [
'views_block__alerts_footer_alerts' => 'openy_rose_views_block__alerts_footer_alerts',
'views_block__alerts_footer_alerts_local' => 'openy_rose_views_block__alerts_footer_alerts_local',
'views_block__alerts_header_alerts' => 'openy_rose_views_block__alerts_header_alerts',
'views_block__alerts_header_alerts_local' =>'openy_rose_views_block__alerts_header_alerts_local',
];
foreach ($configs as $old_id => $new_id) {
\Drupal::configFactory()
->rename('block.block.' . $old_id, 'block.block.' . $new_id);
\Drupal::configFactory()->getEditable('block.block.' . $new_id)
->set('id', $new_id)
->save(TRUE);
}
}
/**
* Update permissions. Remove old configs.
*/
function openy_node_alert_update_8009() {
// Enable modules one more time if 8007 already performed on site.
\Drupal::service('module_installer')->install(['rest', 'hal', 'serialization']);
$configs_to_remove = [
'block.block.views_block__alerts_footer_alerts',
'block.block.views_block__alerts_footer_alerts_local',
'block.block.views_block__alerts_header_alerts',
'block.block.views_block__alerts_header_alerts_local',
];
// Prepare list of alert block configs to remove for currently installed themes.
/** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
$theme_handler = \Drupal::service('theme_handler');
$list = $theme_handler->listInfo();
foreach ($list as $theme => $info) {
$configs_to_remove[] = 'block.block.' . $theme . '_views_block__alerts_footer_alerts';
$configs_to_remove[] = 'block.block.' . $theme . '_views_block__alerts_footer_alerts_local';
$configs_to_remove[] = 'block.block.' . $theme . '_views_block__alerts_header_alerts';
$configs_to_remove[] = 'block.block.' . $theme . '_views_block__alerts_header_alerts_local';
}
$configs_to_remove[] = 'views.view.alerts';
foreach ($configs_to_remove as $config) {
\Drupal::configFactory()->getEditable($config)->delete();
}
openy_node_alert_install();
}
/**
* Update alert nodes. Alerts shouldn't have content moderation enabled to succeed.
*/
function openy_node_alert_update_8010() {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'core.base_field_override.node.alert.promote',
'core.entity_form_display.node.alert.default',
'core.entity_view_display.node.alert.default',
'core.entity_view_display.node.alert.node_alert_footer',
'core.entity_view_display.node.alert.node_alert_header',
'core.entity_view_display.node.alert.teaser',
'core.entity_view_mode.node.node_alert_footer',
'core.entity_view_mode.node.node_alert_header',
'field.field.node.alert.field_alert_belongs',
'field.field.node.alert.field_alert_color',
'field.field.node.alert.field_alert_description',
'field.field.node.alert.field_alert_icon_color',
'field.field.node.alert.field_alert_link',
'field.field.node.alert.field_alert_place',
'field.field.node.alert.field_alert_text_color',
'field.field.node.alert.field_alert_visibility_pages',
'field.field.node.alert.field_alert_visibility_state',
'field.storage.node.field_alert_belongs',
'field.storage.node.field_alert_color',
'field.storage.node.field_alert_description',
'field.storage.node.field_alert_icon_color',
'field.storage.node.field_alert_link',
'field.storage.node.field_alert_place',
'field.storage.node.field_alert_text_color',
'field.storage.node.field_alert_visibility_pages',
'field.storage.node.field_alert_visibility_state',
'node.type.alert',
'pathauto.pattern.alert',
'rabbit_hole.behavior_settings.node_type_alert',
'views.view.alert_belongs_reference',
'rest.resource.alerts_rest_resource'
];
// Import new configuration.
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
$alerts = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => 'alert']);
/** @var \Drupal\node\Entity\Node $alert */
foreach ($alerts as $alert) {
if ($alert->hasField('field_alert_belongs')) {
if ($alert->hasField('field_alert_visibility_pages') && $alert->hasField('field_alert_visibility_state')) {
$pages = $alert->get('field_alert_visibility_pages')->value;
$state = $alert->get('field_alert_visibility_state')->value;
if (!$state) {
$state = 'include';
}
$pages = mb_strtolower($pages);
$path_matcher = \Drupal::service('path.matcher');
$refid = $alert->field_alert_belongs->target_id;
if (!$refid) {
$alert->set('field_alert_visibility_state', $state);
$alert->save();
continue;
}
$alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $refid);
$is_path_match = $path_matcher->matchPath($alias, $pages);
if (!$is_path_match) {
if ($pages) {
$pages = $alias . "\n" . $pages;
}
else {
$pages = $alias;
}
$alert->set('field_alert_visibility_state', $state);
$alert->set('field_alert_visibility_pages', $pages);
$alert->set('field_alert_belongs', NULL);
$alert->save();
}
}
else {
\Drupal::logger(__FUNCTION__)->emergency('There are no fields to migrate into, skipping...');
}
}
}
}
/**
* Restore field_alert_belongs if not exist.
*/
function openy_node_alert_update_8011() {
// Note: no need to check field existing, those configs anyway was changed
// and should be imported.
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'core.entity_form_display.node.alert.default',
'core.entity_view_display.node.alert.default',
'core.entity_view_display.node.alert.node_alert_footer',
'core.entity_view_display.node.alert.node_alert_header',
'core.entity_view_display.node.alert.teaser',
'core.entity_view_mode.node.node_alert_footer',
'core.entity_view_mode.node.node_alert_header',
'field.field.node.alert.field_alert_belongs',
'field.storage.node.field_alert_belongs',
];
// Import updated configuration.
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
}
/**
* Add functionality for Rearranging Alerts.
*/
function openy_node_alert_update_8012() {
openy_node_alert_update_fixes();
\Drupal::service('module_installer')->install(['draggableviews']);
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'views.view.alerts_rearrange',
];
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
}
/**
* Update Open Y Node Alert feature to use link attributes widget.
*/
function openy_node_alert_update_8013() {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
// Update multiple configurations.
$configs = [
'core.entity_form_display.node.alert.default' =>[
'content.field_alert_link.settings.enabled_attributes',
'content.field_alert_link.type',
],
];
$config_updater = \Drupal::service('openy_upgrade_tool.param_updater');
foreach ($configs as $config_name => $params) {
$config = $config_dir . $config_name . '.yml';
foreach ($params as $param) {
$config_updater->update($config, $config_name, $param);
}
}
}
/**
* Add location reference field for alerts.
*/
function openy_node_alert_update_8014() {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'field.field.node.alert.field_alert_location',
'field.storage.node.field_alert_location',
];
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
$configs = [
'core.entity_form_display.node.alert.default' => [
'dependencies.config',
'content.field_alert_location',
],
'core.entity_view_display.node.alert.default' => [
'dependencies.config',
'content.field_alert_location',
],
'core.entity_view_display.node.alert.node_alert_footer' => [
'dependencies.config',
'hidden.field_alert_location',
],
'core.entity_view_display.node.alert.node_alert_header' => [
'dependencies.config',
'hidden.field_alert_location',
],
'core.entity_view_display.node.alert.teaser' => [
'dependencies.config',
'hidden.field_alert_location',
],
];
$config_updater = \Drupal::service('openy_upgrade_tool.param_updater');
foreach ($configs as $config_name => $params) {
$config = $config_dir . $config_name . '.yml';
foreach ($params as $param) {
$config_updater->update($config, $config_name, $param);
}
}
}
/**
* Fix location reference field for alerts.
*/
function openy_node_alert_update_8016() {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'field.field.node.alert.field_alert_location' => [
'settings'
]
];
$config_updater = \Drupal::service('openy_upgrade_tool.param_updater');
foreach ($configs as $config_name => $params) {
$config = $config_dir . $config_name . '.yml';
foreach ($params as $param) {
$config_updater->update($config, $config_name, $param);
}
}
}
/**
* Fix visibility pages and state help text for alerts.
*/
function openy_node_alert_update_8017() {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'field.field.node.alert.field_alert_visibility_pages' => [
'description'
],
'field.field.node.alert.field_alert_visibility_state' => [
'description'
]
];
$config_updater = \Drupal::service('openy_upgrade_tool.param_updater');
foreach ($configs as $config_name => $params) {
$config = $config_dir . $config_name . '.yml';
foreach ($params as $param) {
$config_updater->update($config, $config_name, $param);
}
}
}
/**
* Update Rearranging Alerts views configuration.
*/
function openy_node_alert_update_8018() {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'views.view.alerts_rearrange',
];
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
}
/**
* Add a new field for CT Class to store a type of class.
*/
function openy_node_alert_update_91001(&$sandbox): void {
_openy_node_alert_mark_old_fields_not_required();
_openy_node_alert_add_new_alert_style_field();
_openy_node_alert_update_existing_alerts($sandbox);
}
/**
* Add additional Alert Style options.
*/
function openy_node_alert_update_91002(): void
{
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'field.storage.node.field_alert_style',
];
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
}
/**
* Mark the bg-color, text-color, and icon-color fields not required.
*/
function _openy_node_alert_mark_old_fields_not_required(): void {
$config_dir = \Drupal::service('extension.list.module')
->getPath('openy_node_alert') . '/config/install/';
$config_updater = \Drupal::service('openy_upgrade_tool.param_updater');
$configs = [
'field.field.node.alert.field_alert_color',
'field.field.node.alert.field_alert_icon_color',
'field.field.node.alert.field_alert_text_color',
];
foreach ($configs as $config_name) {
$config_file = $config_dir . $config_name . '.yml';
$config_updater->update($config_file, $config_name, 'required');
}
}
/**
* Add new field Alert Style for the CT Alert.
*/
function _openy_node_alert_add_new_alert_style_field(): void {
$config_dir = \Drupal::service('extension.list.module')->getPath('openy_node_alert') . '/config/install/';
$configs = [
'field.storage.node.field_alert_style',
'field.field.node.alert.field_alert_style',
'core.entity_form_display.node.alert.default',
];
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs($configs);
}
/**
* Set a value of a new field Alert Style to 'classic' for existing nodes.
*/
function _openy_node_alert_update_existing_alerts(&$sandbox): TranslatableMarkup {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current'] = 0;
$sandbox['max'] = \Drupal::entityQuery('node')
->condition('type', 'alert')
->count()
->accessCheck()
->execute();
}
// Load Alert entity in chunks of 20 entities.
$ids = \Drupal::entityQuery('node')
->condition('type', 'alert')
->condition('nid', $sandbox['current'], '>')
->range(0, 20)
->sort('nid')
->accessCheck()
->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($ids);
foreach ($nodes as $node) {
if (!$node->hasField('field_alert_style')
&& !$node->get('field_alert_style')->isEmpty()) {
continue;
}
$node->set('field_alert_style', 'classic');
$node->save();
$sandbox['progress']++;
$sandbox['current'] = $node->id();
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
return t(' @count Alert nodes were updated', ['@count' => $sandbox['max']]);
}