-
Notifications
You must be signed in to change notification settings - Fork 40
/
cohesion.install
executable file
·2103 lines (1860 loc) · 66 KB
/
cohesion.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Drupal\Core\Database\Database;
use Drupal\Component\Serialization\Yaml as Yml;
use Drupal\cohesion_custom_styles\Entity\CustomStyleType;
use Drupal\cohesion_website_settings\Entity\WebsiteSettings;
use Drupal\cohesion_website_settings\Entity\Color;
use Drupal\cohesion_website_settings\Entity\IconLibrary;
use Drupal\cohesion_website_settings\Entity\FontLibrary;
use Drupal\cohesion_website_settings\Entity\FontStack;
use Drupal\Component\Serialization\Json;
use Drupal\cohesion\Entity\CohesionSettingsInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\filter\Plugin\Filter\FilterHtml;
/**
* Implements hook_requirements().
*/
function cohesion_requirements($phase) {
$requirements = [];
$moduleHandler = \Drupal::moduleHandler();
if (!$moduleHandler->moduleExists('node_revision_delete')) {
$settings = [
'@module_link' => 'https://www.drupal.org/project/node_revision_delete',
'@docs_link' => 'https://sitestudiodocs.acquia.com/user-guide/managing-revisions'
];
$requirements['cohesion_node_delete'] = [
'title' => t('Site Studio'),
'value' => t('It is recommended that the node revision delete module is installed to help manage revisions and keep your database to a reasonable size.'),
'description' => t('Please download and configure <a href="@module_link">node revision delete</a>. Find out more about <a href="@docs_link">managing revisions</a> in Site Studio.', $settings),
'severity' => REQUIREMENT_WARNING,
];
}
$submodule_installed = $moduleHandler->moduleExists('sitestudio_contenthub_publisher') || $moduleHandler->moduleExists('sitestudio_contenthub_subscriber');
if ($moduleHandler->moduleExists('acquia_contenthub') && !$submodule_installed) {
$settings = ['@link' => 'https://github.com/acquia/sitestudio_contenthub'];
$requirements['cohesion_contenthub'] =[
'title' => t('Site Studio'),
'description' => t('The Acquia Content Hub integration with Site Studio has changed, please download and configure <a href="@link">sitestudio_contenthub</a>', $settings),
'severity' => REQUIREMENT_WARNING,
];
}
if (!$moduleHandler->moduleExists('ckeditor5') && $phase == 'runtime') {
$settings = ['@link' => '/admin/modules', '@text_format' => '/admin/config/content/formats'];
$requirements['cohesion_ckeditor5'] = [
'title' => t('Site Studio'),
'description' => t('The CKEditor 5 module is a requirement of Site Studio version 7.x, please ensure this <a href="@link">module is enabled</a> and at least one CKEditor 5 <a href="@text_format">text format</a> is created', $settings),
'severity' => REQUIREMENT_ERROR,
];
}
return $requirements;
}
/**
* Implements hook_install().
*/
function cohesion_install() {
// Remove/create cohesion base directory.
_cohesion_create_directory(COHESION_FILESYSTEM_URI, 'Site Studio directory created');
}
/**
* Update the database.
*/
function cohesion_update_8101(&$sandbox) {
$spec = [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
'description' => 'Form element element type.',
];
$schema = Database::getConnection()->schema();
$schema->addField('coh_element_schema_info', 'element_element', $spec);
}
/**
* Implements hook_schema().
*
* Create cohesion database tables.
*/
function cohesion_schema() {
return [
'coh_element_schema_info' => [
'description' => 'Definition and settings for Site Studio assets retrieved from the API.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique schema ID.',
],
'element_id' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Unique ID of form element.',
],
'element_label' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Friendly label for the element.',
],
'element_group' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Group to which the element belongs.',
],
'element_weight' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
'description' => 'Weight of the item on the display.',
],
'element_element' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
'description' => 'Form element element type.',
],
'feature_id' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Form element feature ID.',
],
],
'primary key' => ['id'],
'indexes' => [
'element_group' => ['element_group'],
'element_id' => ['element_id'],
'feature_id' => ['feature_id'],
],
'unique_keys' => ['element_group', 'element_id'],
],
'coh_usage' => [
'description' => 'Site Studio usage tracking.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique schema ID.',
],
'source_uuid' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'UUID of the source entity.',
],
'source_type' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Type of the source entity',
],
'requires_uuid' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'UUID of the required entity.',
],
'requires_type' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Type of the required entity',
],
],
'primary key' => ['id'],
'indexes' => [
'id' => ['id'],
'source_uuid' => ['source_uuid']
],
'unique_keys' => ['id'],
],
];
}
/**
* Alter Site Studio layout field schema 'type' property from 'varchar' to
* 'Text'(Affected columns: 'json_schema', 'json_values', 'json_mapper') Update
* the database.
*/
function cohesion_update_8102(&$sandbox) {
// Get cohesion layout fields
$schema = Database::getConnection()->schema();
$spec = ['type' => 'text', 'size' => 'big', 'not null' => FALSE];
$alterColumn = function (Drupal\Core\Database\Schema $schema, $table_name, $columns, $spec) {
if ($columns && $table_name) {
foreach ($columns as $column) {
if ($schema->fieldExists($table_name, $column)) {
$schema->changeField($table_name, $column, $column, $spec);
}
}
}
};
foreach (\Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadMultiple() as $bundle_field) {
if ($bundle_field instanceof Drupal\field\Entity\FieldStorageConfig && 'cohesion_layout' == $bundle_field->get('type')) {
$profile_config_target_parts = explode('.', $bundle_field->getConfigTarget());
$column_prefix = end($profile_config_target_parts);
$table_name = str_replace('.', '__', $bundle_field->getConfigTarget());
$revision_table_name = $bundle_field->isRevisionable() ? str_replace('.', '_revision__', $bundle_field->getConfigTarget()) : FALSE;
$column_names = [
$column_prefix . '_json_schema',
$column_prefix . '_json_values',
$column_prefix . '_json_mapper',
];
$alterColumn($schema, $table_name, $column_names, $spec);
$alterColumn($schema, $revision_table_name, $column_names, $spec);
}
}
}
/**
* Update cohesion admin views page for cohesion_components_admin,
* cohesion_helpers_admin, cohesion_master_templates_list, custom_styles and
* view_templates_list.
*/
function cohesion_update_8103(&$variables) {
_update_cohesion_admin_list_views();
}
/**
* Update cohesion admin views page for cohesion_master_templates_list and
* view_templates_list.
*/
function cohesion_update_8104(&$variables) {
$path = \Drupal::service('module_handler')->getModule('cohesion')->getPath();
$views_list = array_filter(\Drupal::service('config.factory')
->listAll('views.'), function ($value) {
return (in_array(
$value, [
'views.view.cohesion_master_templates_list',
'views.view.view_templates_list',
]
));
});
foreach ($views_list as $profile_config_id) {
$profile_config = \Drupal::service('config.factory')
->getEditable($profile_config_id);
if (file_exists(DRUPAL_ROOT . '/' . $path)) {
$files = [];
$dir = DRUPAL_ROOT . '/' . $path;
try {
if (is_dir($dir)) {
$files = \Drupal::service('file_system')->scanDirectory($dir, "/{$profile_config_id}.yml$/", ['recurse' => TRUE]);
}
}
catch (FileException $e) {
// Ignore and return empty array for BC.
}
$file = reset($files);
$data = Yml::decode(file_get_contents($file->uri));
$profile_config->merge($data);
if ($profile_config->save()) {
\Drupal::messenger()->addMessage(t("Success: @config updated", ['@config' => $profile_config->getName(),]));
}
}
}
}
/**
* Alter Site Studio layout field schema to add the 'styles' and 'template' fields.
* Update the database.
*/
function cohesion_update_8113(&$sandbox) {
// Get cohesion layout fields
$schema = Database::getConnection()->schema();
$spec = ['type' => 'text', 'size' => 'big', 'not null' => FALSE];
$addColumn = function (Drupal\Core\Database\Schema $schema, $table_name, $column_prefix, $spec) {
if ($table_name) {
// Add styles field.
if (!$schema->fieldExists($table_name, $column_prefix . '_styles')) {
$schema->addField($table_name, $column_prefix . '_styles', $spec);
}
// Add template field.
if (!$schema->fieldExists($table_name, $column_prefix . '_template')) {
$schema->addField($table_name, $column_prefix . '_template', $spec);
}
}
};
// Loop through layout_fields and add subfields using the $addColumn() function above.
foreach (\Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadMultiple() as $bundle_field) {
if ($bundle_field instanceof Drupal\field\Entity\FieldStorageConfig && 'cohesion_layout' == $bundle_field->get('type')) {
$profile_config_target_parts = explode('.', $bundle_field->getConfigTarget());
$column_prefix = end($profile_config_target_parts);
$table_name = str_replace('.', '__', $bundle_field->getConfigTarget());
$revision_table_name = $bundle_field->isRevisionable() ? str_replace('.', '_revision__', $bundle_field->getConfigTarget()) : FALSE;
$addColumn($schema, $table_name, $column_prefix, $spec);
$addColumn($schema, $revision_table_name, $column_prefix, $spec);
}
}
}
/**
* Update cohesion admin views page for cohesion_components_admin,
* cohesion_helpers_admin, cohesion_master_templates_list, custom_styles and
* view_templates_list.
*/
function cohesion_update_8114(&$variables) {
_update_cohesion_admin_list_views();
}
/**
* Remove/create dx8asset base directory.
*/
function cohesion_update_8118(&$variables) {
//
}
/**
*
* Remove citation
*/
function cohesion_update_8119(&$variables) {
//Delete custom style citation
try {
\Drupal::service('cohesion.element.storage')->cohDelete('citation', 'custom_styles');
$entity = \Drupal::entityTypeManager()
->getStorage('custom_style_type')
->load('citation');
if ($entity instanceof \Drupal\Core\Config\Entity\ConfigEntityInterface) {
$entity->delete();
}
//Delete base style citation
\Drupal::service('cohesion.element.storage')->cohDelete('citation', 'base_styles');
$entity = \Drupal::entityTypeManager()
->getStorage('cohesion_base_styles')
->load('citation');
if ($entity instanceof \Drupal\Core\Config\Entity\ConfigEntityInterface) {
$entity->delete();
}
} catch (\Exception $e) {
}
}
/**
*
* Update cohesion admin views page for cohesion_components_admin,
* cohesion_helpers_admin, cohesion_master_templates_list, custom_styles and
* view_templates_list.
*/
function cohesion_update_8120(&$variables) {
_update_cohesion_admin_list_views();
}
/**
*
* Update cohesion admin views page for cohesion_components_admin,
* cohesion_helpers_admin, cohesion_master_templates_list, custom_styles and
* view_templates_list.
*/
function cohesion_update_8122(&$variables) {
_update_cohesion_admin_list_views();
}
/**
* Import custom styles and default forms
*/
function cohesion_update_8123(&$variables) {
$groups = [
['element_group' => 'custom_styles'],
['element_group' => 'form_defaults'],
];
$context = [];
foreach ($groups as $group) {
\Drupal::service('settings.endpoint.utils')->importAsset($group, $context);
}
}
/**
* Create custom style types
*/
function cohesion_update_8124(&$variables) {
if (($elements = \Drupal::service('cohesion.element.storage')->getByGroup('custom_styles'))) {
CustomStyleType::importCustomStyleTypeEntities($elements);
}
}
/**
* Merge existing headings(h1...h6) into new heading custom type
*/
function cohesion_update_8125(&$variables) {
// Reset all convert heading elements to new format
$legacy_headings = [
'heading_1',
'heading_2',
'heading_3',
'heading_4',
'heading_5',
'heading_6',
];
// Merge existing headings(h1...h6) into heading
$entities = \Drupal::service('entity_type.manager')
->getStorage('cohesion_custom_style')
->loadMultiple();
foreach ($entities as $entity) {
if (in_array($entity->getCustomStyleType(), $legacy_headings)) {
// convert existing headings to new format
$entity->set('custom_style_type', 'heading');
$entity->save();
}
}
}
/**
* Remove reference to unused custom style groups from keyValue storage and Site Studio
* storage
*/
function cohesion_update_8126(&$variables) {
$legacy_headings = [
'heading_1',
'heading_2',
'heading_3',
'heading_4',
'heading_5',
'heading_6',
];
// Remove from keyValue storage
\Drupal::keyValue('cohesion.assets.custom_styles')
->deleteMultiple($legacy_headings);
// Remove from Site Studio storage
foreach ($legacy_headings as $legacy_heading) {
\Drupal::service('cohesion.element.storage')->cohDelete($legacy_heading, 'custom_styles');
}
}
/**
* Remove unused custom style groups from list
*/
function cohesion_update_8127(&$variables) {
$legacy_headings = [
'heading_1',
'heading_2',
'heading_3',
'heading_4',
'heading_5',
'heading_6',
];
$custom_style_types = Drupal::entityTypeManager()
->getStorage('custom_style_type')
->loadMultiple();
foreach ($custom_style_types as $entity) {
if (
in_array($entity->id(), $legacy_headings) &&
($entity instanceof \Drupal\cohesion_custom_styles\Entity\CustomStyleType)
) {
$entity->delete();
}
}
}
/**
*
* Delete cohesion views
*/
function cohesion_update_8128(&$variables) {
$views_list = array_filter(\Drupal::service('config.factory')
->listAll('views.'), function ($value) {
return (in_array(
$value, [
'views.view.cohesion_components_admin',
'views.view.cohesion_helpers_admin',
'views.view.cohesion_master_templates_list',
'views.view.custom_styles',
'views.view.view_templates_list',
]
));
});
foreach ($views_list as $view) {
\Drupal::service('config.factory')->getEditable($view)->delete();
}
}
/**
* Merge existing headings(h1...h6) into new heading for style helpers
*/
function cohesion_update_8129(&$variables) {
// Reset all convert heading elements to new format
$legacy_headings = [
'heading_1',
'heading_2',
'heading_3',
'heading_4',
'heading_5',
'heading_6',
];
// Merge existing headings(h1...h6) into heading
$entities = \Drupal::service('entity_type.manager')
->getStorage('cohesion_style_helper')
->loadMultiple();
foreach ($entities as $entity) {
if (in_array($entity->getCustomStyleType(), $legacy_headings)) {
// convert existing headings to new format
$entity->set('custom_style_type', 'heading');
$entity->save();
}
}
}
/**
* Remove reference to unused custom style groups from keyValue storage and Site Studio
* storage
*/
function cohesion_update_8130(&$variables) {
$legacy_icons = [
'icon',
];
// Remove from keyValue storage
\Drupal::keyValue('cohesion.assets.custom_styles')
->deleteMultiple($legacy_icons);
// Remove from Site Studio storage
foreach ($legacy_icons as $legacy_icon) {
\Drupal::service('cohesion.element.storage')->cohDelete($legacy_icon, 'custom_styles');
}
}
/**
* Remove unused custom style groups from list
*/
function cohesion_update_8131(&$variables) {
$legacy_icons = [
'icon',
];
$custom_style_types = Drupal::entityTypeManager()
->getStorage('custom_style_type')
->loadMultiple();
foreach ($custom_style_types as $entity) {
if (
in_array($entity->id(), $legacy_icons) &&
($entity instanceof \Drupal\cohesion_custom_styles\Entity\CustomStyleType)
) {
$entity->delete();
}
}
}
/**
* Remove custom element from sidebar list.
*/
function cohesion_update_8501(&$variables) {
// Element.
$legacy_element = [
'custom',
];
// Remove from keyValue storage
\Drupal::keyValue('cohesion.assets.elements')
->deleteMultiple($legacy_element);
// Remove from Site Studio storage
foreach ($legacy_element as $legacy_icon) {
\Drupal::service('cohesion.element.storage')->cohDelete($legacy_element, 'elements');
}
// Element form data.
$legacy_element = [
'custom_dynamic',
'custom_element',
'custom_settings',
];
foreach ($legacy_element as $legacy_item) {
\Drupal::service('cohesion.element.storage')->cohDelete($legacy_item, 'element_properties');
}
// Remove from keyValue storage
$allData = \Drupal::keyValue('cohesion.assets.element_categories')->getAll();
unset($allData['drupal-core-elements']['elements'][6]);
\Drupal::keyValue('cohesion.assets.element_categories')
->set('drupal-core-elements', $allData['drupal-core-elements']);
}
/**
* Create the usage tracking table. Convert website settings to single entities.
*/
function cohesion_update_8502(&$variables) {
$dx8_no_send_to_api = &drupal_static('dx8_no_send_to_api');
$dx8_no_send_to_api = TRUE;
/**
* Delete existing in-use config items and create new usage table.
*/
Database::getConnection()->schema()->dropTable('coh_usage');
$inuse_config = array_filter(\Drupal::service('config.factory')
->listAll(), function ($value) {
return strstr($value, '.inuse.');
});
foreach ($inuse_config as $item) {
\Drupal::service('config.factory')->getEditable($item)->delete();
}
Database::getConnection()->schema()->createTable('coh_usage', cohesion_schema()['coh_usage']);
// Callback to remove existing file usage for the following entities.
$remove_file_usage = function($entities) {
$regex = '/((cohesion):\/\/(.*?))([:"*?<>|\\\])/m';
foreach ($entities as $entity) {
$uris = [];
$json = str_replace('\\/', '/', $entity->getJsonValues()); // Patch the JSON.
preg_match_all($regex, $json, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $match) {
// Found a matching color.
$uris[] = $match[1];
}
foreach ($uris as $uri) {
if ($file_entity = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri])) {
$file_entity = reset($file_entity);
\Drupal::service('file.usage')->delete($file_entity, 'cohesion', 'cohesion_website_settings', $entity->id(), 0);
}
}
}
};
/**
* Convert icon_libraries to single entities.
*/
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_icon_library')->loadMultiple()) {
\Drupal::entityTypeManager()->getStorage('cohesion_icon_library')->delete($entities_to_delete);
}
/** @var WebsiteSettings $entity */
if ($wsentity = \Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->load('icon_libraries')) {
// Create new entities from existing WS entity.
foreach ($wsentity->getDecodedJsonValues()['uploadIconFonts'] as $icon) {
if (isset($icon['library']['name'])) {
if ($existing_entity = \Drupal::entityTypeManager()->getStorage('cohesion_icon_library')->load($icon['library']['fontFamilyName'])) {
\Drupal::entityTypeManager()->getStorage('cohesion_icon_library')->delete([$existing_entity]);
}
$entity = IconLibrary::create([
'id' => $icon['library']['fontFamilyName'],
'label' => $icon['library']['name'],
]);
$entity->setDefaultValues();
$entity->setJsonValue(Json::encode($icon['library']));
$entity->setSource('uploadIconFonts');
$entity->save();
}
}
// Create new entities from existing WS entity.
foreach ($wsentity->getDecodedJsonValues()['iconLibraries'] as $icon) {
if (isset($icon['library']['name'])) {
if ($existing_entity = \Drupal::entityTypeManager()->getStorage('cohesion_icon_library')->load($icon['library']['fontFamilyName'])) {
\Drupal::entityTypeManager()->getStorage('cohesion_icon_library')->delete([$existing_entity]);
}
$entity = IconLibrary::create([
'id' => $icon['library']['fontFamilyName'],
'label' => $icon['library']['name'],
]);
$entity->setDefaultValues();
$entity->setJsonValue(Json::encode($icon['library']));
$entity->setSource('webIconFonts');
$entity->save();
}
}
// Delete the old WS color palette entity.
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->loadMultiple(['icon_libraries'])) {
$remove_file_usage($entities_to_delete);
\Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->delete($entities_to_delete);
}
}
/**
* Convert font_libraries and font_stacks to single entities.
*/
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_font_library')->loadMultiple()) {
\Drupal::entityTypeManager()->getStorage('cohesion_font_library')->delete($entities_to_delete);
}
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_font_stack')->loadMultiple()) {
\Drupal::entityTypeManager()->getStorage('cohesion_font_stack')->delete($entities_to_delete);
}
/** @var WebsiteSettings $entity */
if ($wsentity = \Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->load('font_libraries')) {
// Create new FontLibrary entities from existing WS entity.
foreach ($wsentity->getDecodedJsonValues()['uploadFonts'] as $font) {
$hash = hash('md5', Json::encode($font['library']));
$entity = FontLibrary::create([
'id' => $hash,
'label' => $font['library']['name'],
]);
$font['library']['uid'] = $hash;
$entity->setDefaultValues();
$entity->setJsonValue(Json::encode($font['library']));
$entity->setSource('uploadFonts');
$entity->save();
}
// Create new FontLibrary entities from existing WS entity.
foreach ($wsentity->getDecodedJsonValues()['webFonts'] as $font) {
$hash = hash('md5', Json::encode($font));
$font['uid'] = $hash;
$entity = FontLibrary::create([
'id' => $hash,
'label' => $font['name'],
]);
$entity->setDefaultValues();
$entity->setJsonValue(Json::encode($font));
$entity->setSource('webFonts');
$entity->save();
}
// Create new FontStack entities from existing WS entity.
foreach ($wsentity->getDecodedJsonValues()['fontStacks'] as $stack) {
$entity = FontStack::create([
'id' => $stack['stack']['uid'],
'label' => $stack['stack']['name'],
]);
$entity->setDefaultValues();
$entity->setJsonValue(Json::encode($stack['stack']));
$entity->save();
}
// Delete the old WS font entity.
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->loadMultiple(['font_libraries'])) {
$remove_file_usage($entities_to_delete);
\Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->delete($entities_to_delete);
}
}
/**
* Convert colors to single entities.
*/
// drupal_flush_all_caches(); // This is required for new sites to see the color entity.
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_color')->loadMultiple()) {
\Drupal::entityTypeManager()->getStorage('cohesion_color')->delete($entities_to_delete);
}
/** @var WebsiteSettings $entity */
if ($entity = \Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->load('color_palette')) {
$c = 0;
// Create new entities from existing WS entity.
foreach ($entity->getDecodedJsonValues()['colors'] as $color) {
$entity = Color::create([
'id' => $color['uid'],
'label' => $color['name'],
]);
$entity->setDefaultValues();
$entity->setJsonValue(Json::encode($color));
$entity->setWeight($c++);
$entity->save();
}
// Delete the old WS color palette entity.
if ($entities_to_delete = \Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->loadMultiple(['color_palette'])) {
\Drupal::entityTypeManager()->getStorage('cohesion_website_settings')->delete($entities_to_delete);
}
}
}
/**
* Split model model and mapper from configs
*/
function cohesion_update_8503(){
$dx8_no_send_to_api = &drupal_static('dx8_no_send_to_api');
$dx8_no_send_to_api = TRUE;
$configs = [
'cohesion_component',
'cohesion_helper',
'cohesion_content_templates',
'cohesion_view_templates',
'cohesion_master_templates',
'cohesion_menu_templates',
];
foreach ($configs as $entity_type_name) {
try {
$entity_list = \Drupal::entityTypeManager()
->getStorage($entity_type_name)
->loadMultiple();
foreach ($entity_list as $entity) {
if($entity->get('modified')){
split_model_mapper_entity($entity);
}
}
} catch (\Exception $e) {
}
}
}
/**
* Split model model and mapper from layout canvas
*/
function cohesion_update_8504(){
try {
$query = \Drupal::entityQuery('cohesion_layout')->accessCheck(FALSE);
$entity_ids_all = $query->execute();
$entity_data = \Drupal::service('entity_type.manager')->getStorage('cohesion_layout');
foreach (array_chunk($entity_ids_all, 25) as $entity_ids){
if ($entity_data && ($entities = $entity_data->loadMultiple($entity_ids))) {
/** @var \Drupal\cohesion_elements\Entity\CohesionLayout $entity */
foreach ($entities as $entity) {
// Re-save the entity.
if ($entity instanceof \Drupal\Tests\Core\Entity\RevisionableEntity && $entity->getRevisionId()) {
$entity->setNewRevision(FALSE);
}
split_model_mapper_entity($entity);
}
}
}
} catch (\Exception $e) {
}
}
/**
* Split model model and mapper from translated layout canvas
*/
function cohesion_update_8505(){
try {
$query = \Drupal::entityQuery('cohesion_layout')->accessCheck(FALSE);
$entity_ids_all = $query->execute();
$entity_data = \Drupal::service('entity_type.manager')->getStorage('cohesion_layout');
foreach (array_chunk($entity_ids_all, 25) as $entity_ids){
if ($entity_data && ($entities = $entity_data->loadMultiple($entity_ids))) {
/** @var \Drupal\cohesion_elements\Entity\CohesionLayout $entity */
foreach ($entities as $entity) {
$languages = $entity->getTranslationLanguages(FALSE);
foreach ($languages as $lang_code => $language) {
$translated_entity = $entity->getTranslation($lang_code);
// Re-save the entity.
if ($translated_entity instanceof \Drupal\Tests\Core\Entity\RevisionableEntity && $translated_entity->getRevisionId()) {
$translated_entity->setNewRevision(FALSE);
}
split_model_mapper_entity($translated_entity);
}
}
}
}
} catch (\Exception $e) {
}
}
/**
* Split model model and mapper from layout canvas revisions
*/
function cohesion_update_8506(){
try {
$connection = \Drupal::service('database');
$offset = 0;
$limit = 25;
while (true){
$query = $connection->query("SELECT id, json_values FROM cohesion_layout_field_revision LIMIT :limit OFFSET :offset", [':limit' => $limit, ':offset' => $offset]);
$revisions = $query->fetchAll();
if(!empty($revisions)){
foreach ($revisions as $revision){
$json_values = json_decode($revision->json_values);
process_json_values_split($json_values);
$update = $connection->update('cohesion_layout_field_revision')
->fields([
'json_values' => json_encode($json_values),
])
->condition('id', $revision->id, '=')
->execute();
}
$offset += $limit;
}else{
break;
}
}
} catch (\Exception $e) {
}
}
/**
* Remove legacy hash key from Site Studio config entities.
*/
function cohesion_update_8507() {
$config_factory = \Drupal::configFactory();
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type => $definition) {
if ($definition->entityClassImplements(CohesionSettingsInterface::class)) {
foreach ($config_factory->listAll($definition->getConfigPrefix()) as $config_name) {
$config = $config_factory->getEditable($config_name);
$config->clear('hash');
$config->save(TRUE);
}
}
}
}
/**
* Update model for entity reference element
*/
function cohesion_update_8508(){
update_layout_canvas_model_element('update_entity_reference_model_by_json_values');
}
/**
* Convert all basic elements to standard elements for flex.
*/
function cohesion_update_8509() {
// Convert layout_canvas entities.
update_all_canvases('convert_elements_for_flex');
}
/**
* Remove gradient picker form element
*/
function cohesion_update_8510() {
\Drupal::service('cohesion.element.storage')->cohDelete('form-gradientpicker', 'form_elements');
}
/**
* Install the elements preview image style.
*/
function cohesion_update_8511() {
\Drupal::service('config.installer')->installDefaultConfig('module', 'cohesion_elements');
}
/**
* Set targetContainer to outer for row for columns elements
*/
function cohesion_update_8512 () {
// Convert layout_canvas entities.
update_all_canvases('row_for_columns_attributes');
}