forked from City-of-Helsinki/drupal-module-helfi-tpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelfi_tpr.install
605 lines (531 loc) · 19.4 KB
/
helfi_tpr.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<?php
/**
* @file
* Contains helfi_tpr installation procedures.
*/
declare(strict_types = 1);
use Drupal\content_translation\ContentTranslationHandler;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Helper function to get the content translation field definitions.
*
* @return array
* The translation fields.
*/
function _helfi_tpr_get_content_translation_fields(): array {
$definitions['content_translation_source'] = BaseFieldDefinition::create('language')
->setLabel(t('Translation source'))
->setDescription(t('The source language from which this translation was created.'))
->setDefaultValue(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->setInitialValue(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$definitions['content_translation_outdated'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Translation outdated'))
->setDescription(t('A boolean indicating whether this translation needs to be updated.'))
->setDefaultValue(FALSE)
->setInitialValue(FALSE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$definitions['content_translation_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Translation author'))
->setDescription(t('The author of this translation.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setRevisionable(TRUE)
->setDefaultValueCallback(ContentTranslationHandler::class . '::getDefaultOwnerId')
->setTranslatable(TRUE);
$definitions['content_translation_status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Translation status'))
->setDescription(t('A boolean indicating whether the translation is visible to non-translators.'))
->setDefaultValue(TRUE)
->setInitialValue(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$definitions['content_translation_created'] = BaseFieldDefinition::create('created')
->setLabel(t('Translation created time'))
->setDescription(t('The Unix timestamp when the translation was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$definitions['content_translation_changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Translation changed time'))
->setDescription(t('The Unix timestamp when the translation was most recently saved.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $definitions;
}
/**
* Installs the service entity type.
*/
function helfi_tpr_update_8001() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = \Drupal::entityTypeManager()->getDefinition('tpr_service');
$manager->installEntityType($entity_type);
}
/**
* Install TPR service description field.
*/
function helfi_tpr_update_8002() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$fields = [];
$fields['description'] = BaseFieldDefinition::create('text_with_summary')
->setTranslatable(TRUE)
->setLabel(new TranslatableMarkup('Description'))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
foreach ($fields as $name => $field) {
$manager->installFieldStorageDefinition($name, 'tpr_service', 'helfi_tpr', $field);
}
}
/**
* Install TPR unit service reference field.
*/
function helfi_tpr_update_8003() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$fields = [];
$fields['services'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Services'))
->setSettings([
'target_type' => 'tpr_service',
'handler_settings' => [
'target_bundles' => ['tpr_service'],
],
])
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
foreach ($fields as $name => $field) {
$manager->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field);
}
}
/**
* Install service 'data' field.
*/
function helfi_tpr_update_8004() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$fields = [];
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(new TranslatableMarkup('Data'))
->setDescription(new TranslatableMarkup('A serialized array of additional data.'));
foreach ($fields as $name => $field) {
$manager->installFieldStorageDefinition($name, 'tpr_service', 'helfi_tpr', $field);
}
}
/**
* Install unit 'picture' field.
*/
function helfi_tpr_update_8005() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$fields = [];
$fields['picture_url'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Picture'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE)
->setSettings([
'max_length' => 2048,
'text_processing' => 0,
]);
foreach ($fields as $name => $field) {
$manager->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field);
}
}
/**
* Installs the e-service and service channel entity types.
*/
function helfi_tpr_update_8006() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
foreach (['tpr_errand_service', 'tpr_service_channel'] as $type) {
$entity_type = \Drupal::entityTypeManager()->getDefinition($type);
$manager->installEntityType($entity_type);
}
$fields['errand_services'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Errand Services'))
->setSettings([
'target_type' => 'tpr_errand_service',
'handler_settings' => [
'target_bundles' => ['tpr_errand_service'],
],
])
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
foreach ($fields as $name => $field) {
$manager->installFieldStorageDefinition($name, 'tpr_service', 'helfi_tpr', $field);
}
}
/**
* Enable translation for e-service, service and service channels.
*/
function helfi_tpr_update_8007() : void {
$config_path = drupal_get_path('module', 'helfi_tpr') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$types = ['tpr_errand_service', 'tpr_service', 'tpr_service_channel'];
foreach ($types as $type) {
$config_name = sprintf('language.content_settings.%s.%s', $type, $type);
$config_storage->write($config_name, $source->read($config_name));
}
}
/**
* Installs TPR service links field.
*/
function helfi_tpr_update_8008() : void {
$fields['links'] = BaseFieldDefinition::create('link')
->setLabel(new TranslatableMarkup('Links'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
foreach ($fields as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, 'tpr_service', 'helfi_tpr', $field);
}
}
/**
* Installs content translation fields for errand services and channels.
*/
function helfi_tpr_update_8010() : void {
$definitions = _helfi_tpr_get_content_translation_fields();
foreach (['tpr_errand_service', 'tpr_service_channel'] as $entity_type) {
foreach ($definitions as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, $entity_type, 'helfi_tpr', $field);
}
}
}
/**
* Uninstall all entity types.
*/
function helfi_tpr_update_8011() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_types = [
'tpr_unit',
'tpr_service',
'tpr_errand_service',
'tpr_service_channel',
];
foreach ($entity_types as $type) {
$manager->uninstallEntityType($manager->getEntityType($type));
}
}
/**
* Install all entity types.
*/
function helfi_tpr_update_8012() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_types = [
'tpr_unit',
'tpr_service',
'tpr_errand_service',
'tpr_service_channel',
];
foreach ($entity_types as $type) {
$entity_type = \Drupal::entityTypeManager()->getDefinition($type);
$manager->installEntityType($entity_type);
}
}
/**
* Update tpr_unit and tpr_service views.
*/
function helfi_tpr_update_8016() : void {
$config_path = drupal_get_path('module', 'helfi_tpr') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
// Update TPR service "update action" to configuration.
$tpr_service_action = 'system.action.tpr_service_update_action';
$config_storage->write($tpr_service_action, $source->read($tpr_service_action));
// Create file storages.
$optional_config_path = drupal_get_path('module', 'helfi_tpr') . '/config/optional';
$optional_config_source = new FileStorage($optional_config_path);
$language_config_path = drupal_get_path('module', 'helfi_tpr') . '/config/language/fi';
$language_config_source = new FileStorage($language_config_path);
$views = ['views.view.tpr_service_list', 'views.view.tpr_unit_list'];
// Update Unit and Service views to configuration.
foreach ($views as $view) {
$config_storage->write($view, $optional_config_source->read($view));
}
// Update Unit and Service views Finnish translations to configuration.
foreach ($views as $view) {
$config_storage
->createCollection('language.fi')
->write($view, $language_config_source->read($view));
}
}
/**
* Install 'accessibility_sentences' field.
*/
function helfi_tpr_update_8017() : void {
$field = BaseFieldDefinition::create('tpr_accessibility_sentence')
->setLabel(new TranslatableMarkup('Accessibility sentence'))
->setTranslatable(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('accessibility_sentences', 'tpr_unit', 'helfi_tpr', $field);
}
/**
* Install 'connections' field.
*/
function helfi_tpr_update_8018() : void {
$field = BaseFieldDefinition::create('tpr_connection')
->setLabel(new TranslatableMarkup('Opening hours'))
->setTranslatable(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('opening_hours', 'tpr_unit', 'helfi_tpr', $field);
}
/**
* Install publish and unpublish actions for TPR unit and service.
*/
function helfi_tpr_update_8019() : void {
$config_path = drupal_get_path('module', 'helfi_tpr') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
// Update TPR unit and service publish and unpublish actions to configuration.
$actions = [
'system.action.tpr_service_publish_action',
'system.action.tpr_service_unpublish_action',
'system.action.tpr_unit_publish_action',
'system.action.tpr_unit_unpublish_action',
];
foreach ($actions as $action) {
$config_storage->write($action, $source->read($action));
}
}
/**
* Uninstalls 'created' and 'changed' fields.
*/
function helfi_tpr_update_8020() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_types = [
'tpr_unit',
'tpr_service',
'tpr_errand_service',
'tpr_service_channel',
];
foreach ($entity_types as $type) {
foreach (['created', 'changed'] as $field) {
if (!$definition = $manager->getFieldStorageDefinition($field, $type)) {
continue;
}
$manager->uninstallFieldStorageDefinition($definition);
}
}
}
/**
* Add 'published' and 'owner' entity keys to all TPR entity types.
*/
function helfi_tpr_update_8021() : void {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_types = [
'tpr_unit',
'tpr_service',
'tpr_errand_service',
'tpr_service_channel',
];
foreach ($entity_types as $type) {
$entity_type = $definition_update_manager->getEntityType($type);
$keys = $entity_type->getKeys();
$keys['published'] = 'content_translation_status';
$keys['owner'] = 'content_translation_uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
}
}
/**
* Re-install content_translation_uid and status fields.
*/
function helfi_tpr_update_8022() : void {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_types = [
'tpr_unit',
'tpr_service',
'tpr_errand_service',
'tpr_service_channel',
];
field_purge_batch(1000);
foreach ($entity_types as $type) {
foreach (['uid', 'status'] as $field) {
$field_name = 'content_translation_' . $field;
if ($definition = $manager->getFieldStorageDefinition($field_name, $type)) {
$manager->uninstallFieldStorageDefinition($definition);
field_purge_batch(1000);
$manager->installFieldStorageDefinition($field_name, $type, 'content_translation', $definition);
}
}
}
}
/**
* Update tpr views again.
*/
function helfi_tpr_update_8023() : void {
helfi_tpr_update_8016();
}
/**
* Adds 'menu_link' field to tpr service.
*/
function helfi_tpr_update_8024() : void {
$field = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Menu link'))
->setSettings([
'target_type' => 'menu_link_content',
])
->setRevisionable(FALSE)
->setTranslatable(TRUE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('menu_link_content', 'tpr_service', 'helfi_tpr', $field);
}
/**
* Install tpr_errand_service and tpr_service_channel views.
*/
function helfi_tpr_update_8025() : void {
$config_storage = \Drupal::service('config.storage');
$config_factory = \Drupal::configFactory();
$uuid_service = \Drupal::service('uuid');
// Create file storages.
$config_path = drupal_get_path('module', 'helfi_tpr') . '/config/install';
$config_source = new FileStorage($config_path);
$optional_config_path = drupal_get_path('module', 'helfi_tpr') . '/config/optional';
$optional_config_source = new FileStorage($optional_config_path);
$language_config_path = drupal_get_path('module', 'helfi_tpr') . '/config/language/fi';
$language_config_source = new FileStorage($language_config_path);
// Add new configs for entity publish and unpublish actions.
$actions = [
'system.action.tpr_errand_service_publish_action',
'system.action.tpr_errand_service_unpublish_action',
'system.action.tpr_service_channel_publish_action',
'system.action.tpr_service_channel_unpublish_action',
];
foreach ($actions as $action) {
if (empty($config_factory->loadMultiple([$action]))) {
$config_storage->write($action, $config_source->read($action));
$config_factory->getEditable($action)->set('uuid', $uuid_service->generate())->save();
}
}
// Install new configs for 'Errand service' and 'Service channel' views.
\Drupal::service('config.installer')->installOptionalConfig($optional_config_source);
// Add Finnish translation for 'Errand service' and 'Service channel' views.
$views = [
'views.view.tpr_errand_service_list',
'views.view.tpr_service_channel_list',
];
foreach ($views as $view) {
$config_storage->createCollection('language.fi')->write($view, $language_config_source->read($view));
}
}
/**
* Adds 'provided_languages' field to 'tpr_unit' entity.
*/
function helfi_tpr_update_8026() : void {
$fields['provided_languages'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Provided languages'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setTranslatable(FALSE)
->setDisplayOptions('form', [
'type' => 'readonly_field_widget',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
foreach ($fields as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field);
}
}
/**
* Drop migrate map tables to add second id map field.
*/
function helfi_tpr_update_8027() : void {
foreach (['unit', 'service', 'errand_service', 'service_channel'] as $type) {
$query = \Drupal::database()->schema();
$table = "migrate_map_tpr_$type";
if (!$query->fieldExists($table, 'sourceid2')) {
$query->dropTable($table);
}
}
}
/**
* Make service channels translatable.
*/
function helfi_tpr_update_8028() : void {
drupal_flush_all_caches();
$entityManager = \Drupal::entityTypeManager();
$published = [];
/** @var \Drupal\helfi_tpr\Entity\Channel $entity */
// Delete service channels and store published status to state.
foreach ($entityManager->getStorage('tpr_service_channel')->loadMultiple() as $entity) {
if ($entity->isPublished()) {
$published[] = $entity->id();
}
$entity->delete(TRUE);
}
\Drupal::state()->set('published_service_channels', $published);
// Reset errand service channels.
foreach ($entityManager->getStorage('tpr_errand_service')->loadMultiple() as $entity) {
$entity->setData('channels', [])
->save();
}
}
/**
* Add 'hide_description' field to the TPR Unit entity.
*/
function helfi_tpr_update_8029() : void {
$fields['hide_description'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Hide description'))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
foreach ($fields as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field);
}
}
/**
* Install tpr_ontology_word_details entity type.
*/
function helfi_tpr_update_8030() : void {
// Install entity type.
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = \Drupal::entityTypeManager()->getDefinition('tpr_ontology_word_details');
$manager->installEntityType($entity_type);
// Install content translation fields.
$type = 'tpr_ontology_word_details';
$definitions = _helfi_tpr_get_content_translation_fields();
foreach ($definitions as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, $type, 'helfi_tpr', $field);
}
}
/**
* Re-install school_details field.
*/
function helfi_tpr_update_8031() : void {
drupal_flush_all_caches();
$manager = \Drupal::entityDefinitionUpdateManager();
if ($definition = $manager->getFieldStorageDefinition('school_details', 'tpr_ontology_word_details')) {
// Removes all field data. However, the data is fetched using
// tpr_ontology_word_details migration so importing it again should be easy.
$manager->uninstallFieldStorageDefinition($definition);
$manager->installFieldStorageDefinition('school_details', 'tpr_ontology_word_details', 'helfi_tpr', $definition);
}
}
/**
* Add 'show_www' field to the TPR Unit entity.
*/
function helfi_tpr_update_8032() : void {
$fields['show_www'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Show website link'))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
foreach ($fields as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field);
}
}