-
Notifications
You must be signed in to change notification settings - Fork 0
/
os2web_pagebuilder.install
490 lines (439 loc) · 18.3 KB
/
os2web_pagebuilder.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
<?php
/**
* @file
* Install, uninstall and update hooks for page builder module.
*/
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\Yaml\Yaml;
use Drupal\field\Entity\FieldConfig;
/**
* Reads in new configuration.
*
* @param string $config_name
* Configuration name.
*/
function os2web_pagebuilder_read_in_new_config($config_name, $moduleName = '') {
$path = drupal_get_path('module', $moduleName ?: 'os2web_pagebuilder');
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
$active_storage->write($config_name, Yaml::parse(file_get_contents($path . '/config/optional/' . $config_name . '.yml')));
}
/**
* Updates Indholdside.
*/
function os2web_pagebuilder_update_8001() {
$path = drupal_get_path('module', 'os2web_pagebuilder');
// Create field storage.
$field_media_image_storage_yml = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.media.field_media_image.yml'));
if (!FieldStorageConfig::loadByName($field_media_image_storage_yml['entity_type'], $field_media_image_storage_yml['field_name'])) {
FieldStorageConfig::create($field_media_image_storage_yml)->save();
}
// Create field instance.
$field_media_image_yml = Yaml::parse(file_get_contents($path . '/config/optional/field.field.media.image.field_media_image.yml'));
if (!FieldConfig::loadByName($field_media_image_yml['entity_type'], $field_media_image_yml['bundle'], $field_media_image_yml['field_name'])) {
FieldConfig::create($field_media_image_yml)->save();
}
}
/**
* Updates Indholdside.
*/
function os2web_pagebuilder_update_8002() {
\Drupal::service('module_installer')->install(['media_library']);
os2web_pagebuilder_update_entity_from_yml('os2web_pagebuilder', 'core.entity_form_display.node.os2web_page.default', 'entity_form_display', 'node.os2web_page.default', [
'field_os2web_page_contact' => 'content.field_os2web_page_contact',
'field_os2web_page_contact_hide' => 'content.field_os2web_page_contact_hide',
'field_os2web_page_description' => 'content.field_os2web_page_description',
'field_os2web_page_heading' => 'content.field_os2web_page_heading',
'field_os2web_page_image' => 'content.field_os2web_page_image',
'field_os2web_page_intro' => 'content.field_os2web_page_intro',
'field_os2web_page_kle' => 'content.field_os2web_page_kle',
'field_os2web_page_paragraph_bann' => 'content.field_os2web_page_paragraph_bann',
'field_os2web_page_paragraph_left' => 'content.field_os2web_page_paragraph_left',
'field_os2web_page_paragraph_narr' => 'content.field_os2web_page_paragraph_narr',
'field_os2web_page_paragraph_righ' => 'content.field_os2web_page_paragraph_righ',
'field_os2web_page_paragraph_wide' => 'content.field_os2web_page_paragraph_wide',
'field_os2web_page_related_hide' => 'content.field_os2web_page_related_hide',
'field_os2web_page_section' => 'content.field_os2web_page_section',
]);
}
/**
* Update Indholdside entity image field.
*/
function os2web_pagebuilder_update_8006() {
// Remove outdated fields.
$fields = [
'field_os2web_page_image',
];
foreach ($fields as $field_name) {
$field = \Drupal::entityTypeManager()
->getStorage('field_config')
->load('node.os2web_page.' . $field_name);
if (empty($field)) {
\Drupal::messenger()->addWarning(t('Field :field not found', [':field' => $field_name]));
continue;
}
$field->delete();
field_purge_field($field);
}
// Add new fields.
$path = drupal_get_path('module', 'os2web_pagebuilder');
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents($path . '/config/install/field.storage.node.field_os2web_page_primaryimage.yml'));
if (!FieldStorageConfig::loadByName($field_storage_yml['entity_type'], $field_storage_yml['field_name'])) {
FieldStorageConfig::create($field_storage_yml)->save();
}
// Create field instance.
$field_yml = Yaml::parse(file_get_contents($path . '/config/install/field.field.node.os2web_page.field_os2web_page_primaryimage.yml'));
if (!FieldConfig::loadByName($field_yml['entity_type'], $field_yml['bundle'], $field_yml['field_name'])) {
FieldConfig::create($field_yml)->save();
}
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_form_display.node.os2web_page.default',
'entity_form_display',
'node.os2web_page.default', [
'field_os2web_page_primaryimage' => 'content.field_os2web_page_primaryimage',
'third_party_settings' => 'third_party_settings',
]);
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_view_display.node.os2web_page.default',
'entity_view_display',
'node.os2web_page.default', [
'field_os2web_page_primaryimage' => 'content.field_os2web_page_primaryimage',
]);
}
/**
* Update Indholdside Overskrift and Intro fields to rich text.
*/
function os2web_pagebuilder_update_8007() {
$fields = [
'field_os2web_page_intro' => [
'table' => 'node__field_os2web_page_intro',
'revision_table' => 'node_revision__field_os2web_page_intro',
'format_col' => 'field_os2web_page_intro_format',
],
'field_os2web_page_heading' => [
'table' => 'node__field_os2web_page_heading',
'revision_table' => 'node_revision__field_os2web_page_heading',
'format_col' => 'field_os2web_page_heading_format',
],
];
$database = \Drupal::database();
foreach ($fields as $field_name => $f) {
$table = $f['table'];
$revision_table = $f['revision_table'];
// Entity type here.
$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)) {
continue;
}
// 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'] = 'text_long';
$new_fields[] = $new_field;
// Delete field.
$field->delete();
}
// Step 4: Create new storage configs from existing.
$new_field_storage = $field_storage->toArray();
$new_field_storage['type'] = 'text_long';
$new_field_storage['module'] = 'text';
// 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;
$row[$f['format_col']] = 'wysiwyg_tekst';
$database->insert($table)->fields($row)->execute();
}
}
if (!is_null($revision_rows)) {
foreach ($revision_rows as $row) {
$row = (array) $row;
$row[$f['format_col']] = 'wysiwyg_tekst';
$database->insert($revision_table)->fields($row)->execute();
}
}
}
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_form_display.node.os2web_page.default',
'entity_form_display',
'node.os2web_page.default', [
'field_os2web_page_intro' => 'content.field_os2web_page_intro',
'field_os2web_page_heading' => 'content.field_os2web_page_heading',
]);
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_view_display.node.os2web_page.default',
'entity_view_display',
'node.os2web_page.default', [
'field_os2web_page_intro' => 'content.field_os2web_page_intro',
'field_os2web_page_heading' => 'content.field_os2web_page_heading',
]);
}
/**
* Update Indholdside Overskrift and Intro fields to plain text.
*/
function os2web_pagebuilder_update_8008() {
$fields = [
'field_os2web_page_intro' => [
'table' => 'node__field_os2web_page_intro',
'revision_table' => 'node_revision__field_os2web_page_intro',
'value_col' => 'field_os2web_page_intro_value',
'format_col' => 'field_os2web_page_intro_format',
],
'field_os2web_page_heading' => [
'table' => 'node__field_os2web_page_heading',
'revision_table' => 'node_revision__field_os2web_page_heading',
'value_col' => 'field_os2web_page_heading_value',
'format_col' => 'field_os2web_page_heading_format',
],
];
$database = \Drupal::database();
foreach ($fields as $field_name => $f) {
$table = $f['table'];
$revision_table = $f['revision_table'];
// Entity type here.
$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)) {
continue;
}
// 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'] = 'string_long';
$new_fields[] = $new_field;
// Delete field.
$field->delete();
}
// Step 4: Create new storage configs from existing.
$new_field_storage = $field_storage->toArray();
$new_field_storage['type'] = 'string_long';
// 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;
unset($row[$f['format_col']]);
$row[$f['value_col']] = strip_tags($row[$f['value_col']]);
$database->insert($table)->fields($row)->execute();
}
}
if (!is_null($revision_rows)) {
foreach ($revision_rows as $row) {
$row = (array) $row;
unset($row[$f['format_col']]);
$row[$f['value_col']] = strip_tags($row[$f['value_col']]);
$database->insert($revision_table)->fields($row)->execute();
}
}
}
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_form_display.node.os2web_page.default',
'entity_form_display',
'node.os2web_page.default', [
'field_os2web_page_intro' => 'content.field_os2web_page_intro',
'field_os2web_page_heading' => 'content.field_os2web_page_heading',
]);
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_view_display.node.os2web_page.default',
'entity_view_display',
'node.os2web_page.default', [
'field_os2web_page_intro' => 'content.field_os2web_page_intro',
'field_os2web_page_heading' => 'content.field_os2web_page_heading',
]);
}
/**
* Adding Keywords taxonomy and field.
*/
function os2web_pagebuilder_update_8009() {
// Adding os2web_keyword vocabulary.
$vid = 'os2web_keyword';
$name = 'Nøgleord';
$vocabularies = Vocabulary::loadMultiple();
if (!isset($vocabularies[$vid])) {
$vocabulary = Vocabulary::create(array(
'vid' => $vid,
'description' => '',
'name' => $name,
));
$vocabulary->save();
}
// Add new field.
$field_name = 'field_os2web_page_keyword';
$path = drupal_get_path('module', 'os2web_pagebuilder');
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents($path . "/config/install/field.storage.node.$field_name.yml"));
if (!FieldStorageConfig::loadByName($field_storage_yml['entity_type'], $field_storage_yml['field_name'])) {
FieldStorageConfig::create($field_storage_yml)->save();
}
// Create field instance.
$field_yml = Yaml::parse(file_get_contents($path . "/config/install/field.field.node.os2web_page.$field_name.yml"));
if (!FieldConfig::loadByName($field_yml['entity_type'], $field_yml['bundle'], $field_yml['field_name'])) {
FieldConfig::create($field_yml)->save();
}
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_form_display.node.os2web_page.default',
'entity_form_display',
'node.os2web_page.default', [
$field_name => "content.$field_name",
]);
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_view_display.node.os2web_page.default',
'entity_view_display',
'node.os2web_page.default', [
$field_name => "content.$field_name",
]);
}
/**
* Adding System page content type and field.
*/
function os2web_pagebuilder_update_8010() {
os2web_pagebuilder_read_in_new_config('node.type.os2web_system_page');
os2web_pagebuilder_read_in_new_config('language.content_settings.node.os2web_system_page');
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_pagebuilder') . "/config/optional/field.storage.node.field_os2web_system_page_content.yml"));
if (!FieldStorageConfig::loadByName('node', 'os2web_system_page', 'field_os2web_system_page_content')) {
FieldStorageConfig::create($field_storage_yml)->save();
}
// Create field instance.
$field_yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_pagebuilder') . "/config/optional/field.field.node.os2web_system_page.field_os2web_system_page_content.yml"));
if (!FieldConfig::loadByName('node', 'os2web_system_page', 'field_os2web_system_page_content')) {
FieldConfig::create($field_yml)->save();
}
os2web_pagebuilder_read_in_new_config('core.entity_form_display.node.os2web_system_page.default');
os2web_pagebuilder_read_in_new_config('core.entity_view_display.node.os2web_system_page.default');
}
/**
* Change system page content field widget.
*/
function os2web_pagebuilder_update_8011() {
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_form_display.node.os2web_system_page.default',
'entity_form_display',
'node.os2web_system_page.default', [
'field_os2web_system_page_content' => 'content.field_os2web_system_page_content'
]);
}
/**
* Update Indholdside add Institution page reference.
*/
function os2web_pagebuilder_update_8012() {
os2web_pagebuilder_read_in_new_config('taxonomy.vocabulary.os2web_instituion');
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_pagebuilder') . "/config/optional/field.storage.node.field_os2web_page_institution.yml"));
if (!FieldStorageConfig::loadByName('node', 'os2web_page', 'field_os2web_page_institution')) {
FieldStorageConfig::create($field_storage_yml)->save();
}
// Create field instance.
$field_yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_pagebuilder') . "/config/optional/field.field.node.os2web_page.field_os2web_page_institution.yml"));
if (!FieldConfig::loadByName('node', 'os2web_page', ' field_os2web_page_institution')) {
FieldConfig::create($field_yml)->save();
}
}
/**
* Removed requirements for image size.
*/
function os2web_pagebuilder_update_8013() {
$path = drupal_get_path('module', 'os2web_pagebuilder');
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
$active_storage->write('field.field.node.os2web_page.field_os2web_page_primaryimage', Yaml::parse(file_get_contents($path . '/config/install/field.field.node.os2web_page.field_os2web_page_primaryimage.yml')));
}
/**
* Adding list view mode.
*/
function os2web_pagebuilder_update_8014() {
$path = drupal_get_path('module', 'os2web_pagebuilder');
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
$active_storage->write('core.entity_view_display.node.os2web_page.list', Yaml::parse(file_get_contents($path . '/config/install/core.entity_view_display.node.os2web_page.list.yml')));
}
/**
* Changing node title collation to utf8mb4_danish_ci.
*/
function os2web_pagebuilder_update_8015() {
$database = \Drupal::database();
$database->query("ALTER TABLE node_field_data MODIFY title VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_danish_ci;");
}
function os2web_pagebuilder_update_8016() {
$path = drupal_get_path('module', 'os2web_pagebuilder');
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents($path . '/config/install/field.storage.node.field_os2web_page_primaryicon.yml'));
if (!FieldStorageConfig::loadByName($field_storage_yml['entity_type'], $field_storage_yml['field_name'])) {
FieldStorageConfig::create($field_storage_yml)->save();
}
// Create field instance.
$field_yml = Yaml::parse(file_get_contents($path . '/config/install/field.field.node.os2web_page.field_os2web_page_primaryicon.yml'));
if (!FieldConfig::loadByName($field_yml['entity_type'], $field_yml['bundle'], $field_yml['field_name'])) {
FieldConfig::create($field_yml)->save();
}
os2web_pagebuilder_update_entity_from_yml(
'os2web_pagebuilder',
'core.entity_form_display.node.os2web_page.default',
'entity_form_display',
'node.os2web_page.default', [
'field_os2web_page_primaryicon' => 'content.field_os2web_page_primaryicon'
]);
}