forked from OS2web/os2web_contact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os2web_contact.install
264 lines (239 loc) · 9.64 KB
/
os2web_contact.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
<?php
/**
* @file
* Install, uninstall and update hooks for os2web_contact module.
*/
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Symfony\Component\Yaml\Yaml;
use Drupal\field\Entity\FieldConfig;
use Drupal\Component\Utility\NestedArray;
/**
* Reads in new configuration.
*
* @param string $config_name
* Configuration name.
*/
function os2web_contact_read_in_new_config($config_name) {
$path = drupal_get_path('module', 'os2web_contact');
/** @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')));
}
/**
* 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_contact_update_entity_from_yml($ymlFileName, $entityType, $id, array $setComponents) {
$yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_contact') . '/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::messenger()->addWarning('Component ' . $key . ' has empty configuration');
continue;
}
$entity->setComponent($key, $value);
}
}
$entity->save();
}
/**
* Updates os2web_contact entity fields.
*/
function os2web_contact_update_8001() {
// Remove outdated fields.
$fields = [
'field_os2web_contact_modal_text',
'field_os2web_contact_text',
'field_os2web_contact_heading',
'field_os2web_contact_modal_head',
];
foreach ($fields as $field_name) {
$field = \Drupal::entityTypeManager()
->getStorage('field_config')
->load('os2web_contact.os2web_contact.' . $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.
$fields = [
'field_os2web_contact_modal_body',
'field_os2web_contact_body',
'field_os2web_contact_heading',
'field_os2web_contact_modal_head',
];
$path = drupal_get_path('module', 'os2web_contact');
foreach ($fields as $field_name) {
// Create field storage.
$field_storage_yml = Yaml::parse(file_get_contents($path . '/config/optional/field.storage.os2web_contact.' . $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/optional/field.field.os2web_contact.os2web_contact.' . $field_name . '.yml'));
if (!FieldConfig::loadByName($field_yml['entity_type'], $field_yml['bundle'], $field_yml['field_name'])) {
FieldConfig::create($field_yml)->save();
}
}
os2web_contact_update_entity_from_yml(
'core.entity_form_display.os2web_contact.os2web_contact.default',
'entity_form_display',
'os2web_contact.os2web_contact.default', [
'field_os2web_contact_modal_body' => 'content.field_os2web_contact_modal_body',
'field_os2web_contact_body' => 'content.field_os2web_contact_body',
'field_os2web_contact_heading' => 'content.field_os2web_contact_heading',
'field_os2web_contact_modal_head' => 'content.field_os2web_contact_modal_head',
]);
os2web_contact_update_entity_from_yml(
'core.entity_view_display.os2web_contact.os2web_contact.default',
'entity_view_display',
'os2web_contact.os2web_contact.default', [
'field_os2web_contact_modal_body' => 'content.field_os2web_contact_modal_body',
'field_os2web_contact_body' => 'content.field_os2web_contact_body',
'field_os2web_contact_heading' => 'content.field_os2web_contact_heading',
'field_os2web_contact_modal_head' => 'content.field_os2web_contact_modal_head',
]);
}
/**
* Renaming texts fields.
*/
function os2web_contact_update_8002() {
os2web_contact_read_in_new_config('field.field.os2web_contact.os2web_contact.field_os2web_contact_body');
os2web_contact_read_in_new_config('field.field.os2web_contact.os2web_contact.field_os2web_contact_more');
}
/**
* Adding phone and email fields.
*/
function os2web_contact_update_8003() {
$fields = [
'field_os2web_contact_phone',
'field_os2web_contact_email',
];
$entity_type = 'os2web_contact';
$entity_bundle = 'os2web_contact';
$path = drupal_get_path('module', 'os2web_contact');
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_contact_update_entity_from_yml('core.entity_form_display.' . $yaml_id, 'entity_form_display', $yaml_id, $components);
os2web_contact_update_entity_from_yml('core.entity_view_display.' . $yaml_id, 'entity_view_display', $yaml_id, $components);
}
}
/**
* Renaming $revision_metadata_keys to D9 defaults.
*/
function os2web_contact_update_8004() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Add the published entity key and revisionable metadata fields to the
// os2web_contact entity type.
$entity_type = $definition_update_manager->getEntityType('os2web_contact');
$entity_type_id = $entity_type->id();
$revision_log_message = BaseFieldDefinition::create('string_long')
->setLabel(t('Revision log message'))
->setDescription(t('Briefly describe the changes you have made.'))
->setRevisionable(TRUE)
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => 25,
'settings' => [
'rows' => 4,
],
]);
$definition_update_manager->installFieldStorageDefinition('revision_log_message', $entity_type_id, $entity_type_id, $revision_log_message);
$old_field_storage_definition = $definition_update_manager->getFieldStorageDefinition('revision_log', 'os2web_contact');
if ($old_field_storage_definition instanceof FieldStorageDefinitionInterface) {
$definition_update_manager->uninstallFieldStorageDefinition($old_field_storage_definition);
}
}
/**
* Adding os2web_contact_open_hours field.
*/
function os2web_contact_update_8005() {
// Enabling the module.
\Drupal::service('module_installer')->install(['office_hours']);
$fields = [
'field_os2web_contact_open_hours',
];
$entity_type = 'os2web_contact';
$entity_bundle = 'os2web_contact';
$path = drupal_get_path('module', 'os2web_contact');
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();
}
}
}
/**
* Adding field_os2web_contact_facebook, os2web_contact_website fields.
*/
function os2web_contact_update_8007() {
$fields = [
'field_os2web_contact_facebook',
'field_os2web_contact_website'
];
$entity_type = 'os2web_contact';
$entity_bundle = 'os2web_contact';
$path = drupal_get_path('module', 'os2web_contact');
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();
}
}
}