-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos2web_hearings.install
464 lines (409 loc) · 18.7 KB
/
os2web_hearings.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
<?php
use Drupal\taxonomy\Entity\Term;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Component\Utility\NestedArray;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\Role;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_install().
*/
function os2web_hearings_install() {
// Creating OS2Web Hearings Type: "Høringer" and "Afgørelser".
Term::create([
'vid' => 'os2web_hearings_type',
'name' => OS2WEB_HEARINGS_TYPE_HEARINGS,
])->save();
Term::create([
'vid' => 'os2web_hearings_type',
'name' => OS2WEB_HEARINGS_TYPE_DECISIONS,
])->save();
// Creating OS2Web Hearings status: "I gang" and "Afsluttet".
Term::create([
'vid' => 'os2web_hearings_status',
'name' => OS2WEB_HEARINGS_STATUS_ACTIVE_NAME,
])->save();
Term::create([
'vid' => 'os2web_hearings_status',
'name' => OS2WEB_HEARINGS_STATUS_CLOSED_NAME,
])->save();
// Adding comments permissions for anonymous user.
$anonym = Role::load('anonymous');
$anonym->grantPermission('post comments');
$anonym->grantPermission('access comments');
$anonym->save();
// Adding comments permissions for authenticated user.
$authenticated = Role::load('authenticated');
$authenticated->grantPermission('post comments');
$authenticated->grantPermission('access comments');
$authenticated->save();
}
/**
* Implements hook_update_N().
*
* Activate all hearing without status
*/
function os2web_hearings_update_8800() {
$ids_empty = \Drupal::entityQuery('node')
->condition('type', 'os2web_hearings_hearing_case')
->notExists('field_os2web_hearings_status')
->execute();
$tids = \Drupal::entityQuery('taxonomy_term')
->condition('vid', 'os2web_hearings_status')
->condition('name', OS2WEB_HEARINGS_STATUS_ACTIVE_NAME)
->execute();
if (empty($tids)) {
\Drupal::logger('os2web_horing')
->error('Term for active hearings status not found');
return;
}
$active_tid = reset($tids);
/** @var \Drupal\node\NodeInterface $node */
foreach (\Drupal\node\Entity\Node::loadMultiple($ids_empty) as $node) {
$node->set('field_os2web_hearings_status', $active_tid);
$node->save();
}
}
/**
* Update Frist field to date and time.
*/
function os2web_hearings_update_8801() {
$database = \Drupal::database();
$field_name = 'field_os2web_hearings_end_date';
$table = 'node__field_os2web_hearings_end_date';
$revision_table = 'node_revision__field_os2web_hearings_end_date';
$entity_type = 'node';
// Step 1: Get field storage.
$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
// Check if field not found.
if (is_null($field_storage)) {
return;
}
// Step 2: Store data.
$rows = NULL;
$revision_rows = NULL;
if ($database->schema()->tableExists($table)) {
// The table data to restore after the update is completed.
$rows = $database->select($table, 'n')->fields('n')->execute()->fetchAll();
$revision_rows = $database->select($revision_table, 'n')->fields('n')->execute()->fetchAll();
}
// Step 3: Save new field configs & delete existing fields.
$new_fields = [];
foreach ($field_storage->getBundles() as $bundle => $label) {
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$new_field = $field->toArray();
$new_field['field_type'] = 'datetime';
$new_fields[] = $new_field;
// Delete field.
$field->delete();
field_purge_field($field);
}
// Step 4: Create new storage configs from existing.
$new_field_storage = $field_storage->toArray();
$new_field_storage['type'] = 'datetime';
$new_field_storage['settings']['datetime_type'] = 'datetime';
// Step 5: Purge deleted fields data.
// This is required to create new fields.
field_purge_batch(250);
// Step 6: Create new fieldstorage.
FieldStorageConfig::create($new_field_storage)->save();
// Step 7: Create new fields for all bundles.
foreach ($new_fields as $new_field) {
$new_field = FieldConfig::create($new_field);
$new_field->save();
}
// Step 8: Restore existing data in fields & revision tables.
if (!is_null($rows)) {
foreach ($rows as $row) {
$row = (array)$row;
if (count(explode('T', $row['field_os2web_hearings_end_date_value'])) == 1) {
$row['field_os2web_hearings_end_date_value'] .= 'T12:00:00';
}
$database->insert($table)->fields($row)->execute();
}
}
if (!is_null($revision_rows)) {
foreach ($revision_rows as $row) {
$row = (array)$row;
if (count(explode('T', $row['field_os2web_hearings_end_date_value'])) == 1) {
$row['field_os2web_hearings_end_date_value'] .= 'T12:00:00';
}
$database->insert($revision_table)->fields($row)->execute();
}
}
os2web_hearings_update_entity_from_yml('core.entity_form_display.node.os2web_hearings_hearing_case.default', 'entity_form_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_end_date' => 'content.field_os2web_hearings_end_date',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.node.os2web_hearings_hearing_case.default', 'entity_view_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_end_date' => 'content.field_os2web_hearings_end_date',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.node.os2web_hearings_hearing_case.default', 'entity_view_display', 'node.os2web_hearings_hearing_case.teaser', [
'field_os2web_hearings_end_date' => 'content.field_os2web_hearings_end_date',
]
);
}
/**
* Update entity from yml.
*
* Some config like form display needs to be updated via config manager.
*
* @param string $ymlFileName
* Yml file name.
* @param string $entityType
* The entity type for this storage.
* @param mixed $id
* The ID of the entity to load.
* @param array $setComponents
* Array of components you want to add.
* - The key will be what we are setting.
* - The value is the key that will be used from the new config file
* (Can have in string for array).
*
* @throws
*/
function os2web_hearings_update_entity_from_yml($ymlFileName, $entityType, $id, array $setComponents)
{
$yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_hearings') . '/config/optional/' . $ymlFileName . '.yml'));
$entity = \Drupal::entityTypeManager()->getStorage($entityType)->load($id);
foreach ($setComponents as $key => $value) {
$parts = explode('.', $value);
if (count($parts) == 1) {
$entity->setComponent($key, $yml[$value]);
}
else {
$value = NestedArray::getValue($yml, $parts);
if (empty($value)) {
\Drupal::service('messenger')->addWarning('Component ' . $key . ' has empty configuration');
continue;
}
$entity->setComponent($key, $value);
}
}
$entity->save();
}
/**
* Add new fields to hearings node type: Email notification, SBSYS case number.
*/
function os2web_hearings_update_8802() {
$fields = [
'field_os2web_hearings_not_email',
'field_os2web_hearings_sbsys_case',
];
$entity_type = 'node';
$entity_bundle = 'os2web_hearings_hearing_case';
$path = drupal_get_path('module', 'os2web_hearings');
foreach ($fields as $field_name) {
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.' . $entity_type . '.' . $field_name . '.yml'));
if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
FieldStorageConfig::create($field_storage_yml)->save();
}
// Create field instance.
$field_instance_yml = Yaml::parse(file_get_contents($path . '/config/optional/field.field.' . $entity_type . '.' . $entity_bundle . '.' . $field_name . '.yml'));
if (!FieldConfig::loadByName($entity_type, $entity_bundle, $field_name)) {
FieldConfig::create($field_instance_yml)->save();
}
// Applying form/view_display settings based on config in yml files.
$components = [
$field_name => 'content.' . $field_name,
'hidden' => 'hidden',
];
$yaml_id = $entity_type . '.' . $entity_bundle . '.default';
os2web_hearings_update_entity_from_yml('core.entity_form_display.' . $yaml_id, 'entity_form_display', $yaml_id, $components);
os2web_hearings_update_entity_from_yml('core.entity_view_display.' . $yaml_id, 'entity_view_display', $yaml_id, $components);
$yaml_id = $entity_type . '.' . $entity_bundle . '.teaser';
os2web_hearings_update_entity_from_yml('core.entity_view_display.' . $yaml_id, 'entity_view_display', $yaml_id, $components);
}
}
/**
* Renaming "SBsys sags nummer" field to "SBsys sagsnummer"
*/
function os2web_hearings_update_8803() {
$path = drupal_get_path('module', 'os2web_hearings');
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
// Updating view.
$active_storage->write('field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_sbsys_case', Yaml::parse(file_get_contents($path . '/config/optional/field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_sbsys_case.yml')));
}
/**
* Adding field "Abud (os2web_hearings_tender)" and OS2Web Hearings Tender tax.
*/
function os2web_hearings_update_8804() {
$path = drupal_get_path('module', 'os2web_hearings');
// Creating new taxonomy - os2web_hearings_tender.
$vid = "os2web_hearings_tender";
$name = "OS2Web Hearing tender";
$vocabularies = Vocabulary::loadMultiple();
if (!isset($vocabularies[$vid])) {
$vocabulary = Vocabulary::create(array(
'vid' => $vid,
//'machine_name' => $vid,
'description' => '',
'name' => $name,
));
$vocabulary->save();
}
// Create field storage.
$storageConfug = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.node.field_os2web_hearings_tender.yml'));
if (!FieldStorageConfig::loadByName($storageConfug['entity_type'], $storageConfug['field_name'])) {
FieldStorageConfig::create($storageConfug)->save();
}
// Create field instance.
$fieldConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_tender.yml'));
if (!FieldConfig::loadByName($fieldConfig['entity_type'], $fieldConfig['bundle'], $fieldConfig['field_name'])) {
FieldConfig::create($fieldConfig)->save();
}
}
/**
* Adding Hearing view "Search with tender".
*/
function os2web_hearings_update_8805() {
$path = drupal_get_path('module', 'os2web_hearings');
// Create field storage - field_os2web_hearings_search_typ.
$storageConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.paragraph.field_os2web_hearings_search_typ.yml'));
// Adjusting allowed values options.
$allowed_values = [];
foreach ($storageConfig['settings']['allowed_values'] as $option) {
$allowed_values[$option['value']] = $option['label'];
}
$storageConfig['settings']['allowed_values'] = $allowed_values;
if (!FieldStorageConfig::loadByName($storageConfig['entity_type'], $storageConfig['field_name'])) {
FieldStorageConfig::create($storageConfig)->save();
}
// Create field instance - field_os2web_hearings_search_typ.
$fieldConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.field.paragraph.os2web_hearings_search.field_os2web_hearings_search_typ.yml'));
if (!FieldConfig::loadByName($fieldConfig['entity_type'], $fieldConfig['bundle'], $fieldConfig['field_name'])) {
FieldConfig::create($fieldConfig)->save();
}
// Updating view.
$active_storage = \Drupal::service('config.storage');
$active_storage->write('views.view.os2web_hearings', Yaml::parse(file_get_contents($path . '/config/optional/views.view.os2web_hearings.yml')));
}
/**
* Adding new taxonomy os2web_hearings_category.
*/
function os2web_hearings_update_8806() {
$path = drupal_get_path('module','os2web_hearings');
$active_storage = \Drupal::service('config.storage');
$active_storage->write('taxonomy.vocabulary.os2web_hearings_categories', Yaml::parse(file_get_contents($path . '/config/optional/taxonomy.vocabulary.os2web_hearings_categories.yml')));
// Creating OS2Web Hearings Category: "Budget" and "Landzonetilladelse".
Term::create([
'vid' => 'os2web_hearings_categories',
'name' => OS2WEB_HEARINGS_CATEGORIES_BUDGET,
])->save();
Term::create([
'vid' => 'os2web_hearings_categories',
'name' => OS2WEB_HEARINGS_TYPE_LANDZONETILLADELSE,
])->save();
// Create field storage.
$storageConfug = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.node.field_os2web_hearings_category.yml'));
if (!FieldStorageConfig::loadByName($storageConfug['entity_type'], $storageConfug['field_name'])) {
FieldStorageConfig::create($storageConfug)->save();
}
// Create field instance.
$fieldConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_category.yml'));
if (!FieldConfig::loadByName($fieldConfig['entity_type'], $fieldConfig['bundle'], $fieldConfig['field_name'])) {
FieldConfig::create($fieldConfig)->save();
}
os2web_hearings_update_entity_from_yml('core.entity_form_display.node.os2web_hearings_hearing_case.default', 'entity_form_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_category' => 'content.field_os2web_hearings_category',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.node.os2web_hearings_hearing_case.default', 'entity_view_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_category' => 'content.field_os2web_hearings_category',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.node.os2web_hearings_hearing_case.default', 'entity_view_display', 'node.os2web_hearings_hearing_case.teaser', [
'field_os2web_hearings_category' => 'content.field_os2web_hearings_category',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.node.os2web_hearings_hearing_case.default', 'entity_view_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_category' => 'content.field_os2web_hearings_category',
]
);
// Updating view.
$active_storage = \Drupal::service('config.storage');
$active_storage->write('views.view.os2web_hearings', Yaml::parse(file_get_contents($path . '/config/optional/views.view.os2web_hearings.yml')));
// Updating paragraph.
$active_storage = \Drupal::service('config.storage');
$active_storage->write('field.storage.paragraph.field_os2web_hearings_search_typ', Yaml::parse(file_get_contents($path . '/config/optional/field.storage.paragraph.field_os2web_hearings_search_typ.yml')));
}
/**
* Updating label os2web_hearings_category.
*/
function os2web_hearings_update_8807() {
$path = drupal_get_path('module','os2web_hearings');
$active_storage = \Drupal::service('config.storage');
// Updating view.
$active_storage = \Drupal::service('config.storage');
$active_storage->write('views.view.os2web_hearings', Yaml::parse(file_get_contents($path . '/config/optional/views.view.os2web_hearings.yml')));
// Updating field label.
$active_storage = \Drupal::service('config.storage');
$active_storage->write('field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_category', Yaml::parse(file_get_contents($path . '/config/optional/field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_category.yml')));
// Updating paragraph.
$active_storage = \Drupal::service('config.storage');
$active_storage->write('field.storage.paragraph.field_os2web_hearings_search_typ', Yaml::parse(file_get_contents($path . '/config/optional/field.storage.paragraph.field_os2web_hearings_search_typ.yml')));
}
/**
* Adding field field_os2web_hearings_reply_sp_r.
*/
function os2web_hearings_update_8808() {
// Creating new taxonomy - os2web_hearings_tender.
$vid = "os2web_hearings_speaker_role";
$name = "OS2Web Hearings speaker role";
$vocabularies = Vocabulary::loadMultiple();
if (!isset($vocabularies[$vid])) {
$vocabulary = Vocabulary::create(array(
'vid' => $vid,
'description' => '',
'name' => $name,
));
$vocabulary->save();
}
$path = drupal_get_path('module','os2web_hearings');
// Create field storage.
$storageConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.comment.field_os2web_hearings_reply_sp_r.yml'));
if (!FieldStorageConfig::loadByName($storageConfig['entity_type'], $storageConfig['field_name'])) {
FieldStorageConfig::create($storageConfig)->save();
}
// Create field instance.
$fieldConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.field.comment.os2web_hearings_hearing_reply.field_os2web_hearings_reply_sp_r.yml'));
if (!FieldConfig::loadByName($fieldConfig['entity_type'], $fieldConfig['bundle'], $fieldConfig['field_name'])) {
FieldConfig::create($fieldConfig)->save();
}
os2web_hearings_update_entity_from_yml('core.entity_form_display.comment.os2web_hearings_hearing_reply.default', 'entity_form_display', 'comment.os2web_hearings_hearing_reply.default', [
'field_os2web_hearings_reply_sp_r' => 'content.field_os2web_hearings_reply_sp_r',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.comment.os2web_hearings_hearing_reply.default', 'entity_view_display', 'comment.os2web_hearings_hearing_reply.default', [
'field_os2web_hearings_reply_sp_r' => 'content.field_os2web_hearings_reply_sp_r',
]
);
}
/**
* Adding field field_os2web_hearings_sp_rol_ena.
*/
function os2web_hearings_update_8809() {
$path = drupal_get_path('module','os2web_hearings');
// Create field storage.
$storageConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.node.field_os2web_hearings_sp_rol_ena.yml'));
if (!FieldStorageConfig::loadByName($storageConfig['entity_type'], $storageConfig['field_name'])) {
FieldStorageConfig::create($storageConfig)->save();
}
// Create field instance.
$fieldConfig = Yaml::parse(file_get_contents($path . '/config/optional/field.field.node.os2web_hearings_hearing_case.field_os2web_hearings_sp_rol_ena.yml'));
if (!FieldConfig::loadByName($fieldConfig['entity_type'], $fieldConfig['bundle'], $fieldConfig['field_name'])) {
FieldConfig::create($fieldConfig)->save();
}
os2web_hearings_update_entity_from_yml('core.entity_form_display.node.os2web_hearings_hearing_case.default', 'entity_form_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_sp_rol_ena' => 'content.field_os2web_hearings_sp_rol_ena',
]
);
os2web_hearings_update_entity_from_yml('core.entity_view_display.node.os2web_hearings_hearing_case.default', 'entity_view_display', 'node.os2web_hearings_hearing_case.default', [
'field_os2web_hearings_sp_rol_ena' => 'content.field_os2web_hearings_sp_rol_ena',
]
);
}