-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicms_core_logic.install
More file actions
176 lines (155 loc) · 5.29 KB
/
icms_core_logic.install
File metadata and controls
176 lines (155 loc) · 5.29 KB
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
<?php
/**
* @file
* Update hooks for ICMS core functionality.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
/**
* Implements hook_install().
*/
function icms_core_logic_install() {
module_set_weight('icms_core_logic', 50);
// Attempt to install webform during module installation.
// Note: During site installation with --existing-config, this may be
// overwritten by config import. The icms_admin.webform_install_subscriber
// event subscriber will reinstall webforms after config import completes.
icms_admin_install_webform('webform.webform.contact', 'icms_core_logic');
}
// =============================================================================
// UPDATE HOOKS - Examples
// =============================================================================
// Update hooks run before config import with reduced Drupal API.
// Use for: module enablement, database schema changes.
// Use TranslatableMarkup() instead of t().
//
// Example:
// @code
// function icms_core_logic_update_9001() {
// $config = \Drupal::configFactory()->getEditable('redirect.settings');
// if ($config->get('auto_redirect') !== TRUE) {
// $config->set('auto_redirect', TRUE);
// $config->save();
// return (string) new TranslatableMarkup('Enabled auto_redirect.');
// }
// return (string) new TranslatableMarkup('Auto_redirect was already enabled.');
// }
// @endcode
/**
* Disable the responsive_preview module.
*/
function icms_core_logic_update_9001() {
$module_handler = \Drupal::moduleHandler();
if ($module_handler->moduleExists('responsive_preview')) {
\Drupal::service('module_installer')->uninstall(['responsive_preview']);
return (string) new TranslatableMarkup('Disabled the responsive_preview module.');
}
return (string) new TranslatableMarkup('The responsive_preview module was not installed.');
}
/**
* Clear simple_sitemap base_url setting.
*/
function icms_core_logic_update_9002() {
$config = \Drupal::configFactory()->getEditable('simple_sitemap.settings');
$config->set('base_url', '');
$config->save();
return (string) new TranslatableMarkup('The simple_sitemap base_url was already empty.');
}
/**
* Add fields to paragraph types.
*/
function icms_core_logic_update_9003() {
$fieldNames = [
'field_icms_buttons' => [
'label' => 'Buttons',
'settings' => [
'handler' => 'default:paragraph',
'handler_settings' => [
'target_bundles' => ['icms_button_element'],
'negate' => 0,
],
],
'paragraphs' => [
'icms_layout_downloads',
'icms_layout_partnerships',
'icms_layout_testimonials',
'icms_layout_timeline',
],
],
'field_icms_pretitle' => [
'label' => 'PreTitle',
'settings' => [
],
'paragraphs' => [
'icms_layout_tabs',
],
],
'field_icms_text' => [
'label' => 'Text',
'settings' => [
],
'paragraphs' => [
'icms_layout_tabs',
'icms_layout_testimonials',
],
],
];
// Field order with weights for form display.
$fieldOrder = [
'field_icms_pretitle' => 0,
'field_icms_title' => 1,
'group_title_settings' => 2,
'field_icms_text' => 3,
'field_icms_media_elements' => 4,
'field_icms_buttons' => 5,
];
$log = "";
foreach ($fieldNames as $fieldName => $fieldData) {
$fieldStorage = FieldStorageConfig::loadByName('paragraph', $fieldName);
foreach ($fieldData['paragraphs'] as $paragarphType) {
$field = FieldConfig::loadByName('paragraph', $paragarphType, $fieldName);
if (!$field) {
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $paragarphType,
'label' => $fieldData['label'],
'required' => FALSE,
'settings' => $fieldData['settings'],
]);
$field->save();
$log .= 'Added field ' . $fieldName . ' to paragraph ' . $paragarphType . ". \n";
}
// Add field to form display with proper weight.
$formDisplay = EntityFormDisplay::load('paragraph.' . $paragarphType . '.default');
if ($formDisplay) {
$component = $formDisplay->getComponent($fieldName);
if (!$component) {
$weight = isset($fieldOrder[$fieldName]) ? $fieldOrder[$fieldName] : 10;
$formDisplay->setComponent($fieldName, [
'type' => $fieldStorage->getType() === 'entity_reference_revisions' ? 'paragraphs' : 'string_textfield',
'weight' => $weight,
'settings' => [],
'third_party_settings' => [],
]);
$formDisplay->save();
$log .= 'Added ' . $fieldName . ' to form display for ' . $paragarphType . ' (weight: ' . $weight . "). \n";
}
}
}
}
return (string) new TranslatableMarkup($log ?: 'No changes were needed.');
}
/**
* Install contact webform from config file.
*/
function icms_core_logic_update_9004() {
$success = icms_admin_install_webform('webform.webform.contact', 'icms_core_logic');
if ($success) {
return new TranslatableMarkup('Installed contact webform.');
}
else {
return new TranslatableMarkup('Failed to install contact webform. Check logs for details.');
}
}