-
Notifications
You must be signed in to change notification settings - Fork 4
/
tide_core.install
306 lines (274 loc) · 10.1 KB
/
tide_core.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
<?php
/**
* @file
* Installation functions for Tide Core.
*/
use Drupal\tide_core\TideCoreOperation;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/**
* Implements hook_install().
*/
function tide_core_install() {
// Don't do anything else during config sync.
if (\Drupal::isConfigSyncing()) {
return;
}
// Assign user 1 the "administrator" role.
$user = User::load(1);
$user->roles[] = 'administrator';
$user->save();
// Restrict user registration to admin role creation.
\Drupal::configFactory()
->getEditable('user.settings')
->set('register', UserInterface::REGISTER_ADMINISTRATORS_ONLY)
->save(TRUE);
$tideCoreOperation = new TideCoreOperation();
// Creates terms for Topic vocabulary.
$tideCoreOperation->createTopicTermsVocabulary();
// Creates terms for content_category vocabulary.
$tideCoreOperation->addContentCategoryVocabulary();
// Update default Editorial workflow of Content Moderation.
$tideCoreOperation->updateEditorialWorkflow();
// Update authenticated user permission.
$tideCoreOperation->updateAuthenticatedUserPermission();
// Deletes unsupported actions from /admin/contents view.
$tideCoreOperation->deleteUnsupportedActions();
// Add business contact fields to user account form display.
$tideCoreOperation->addBusinessFieldsToUserAccountForm();
// Use custom files view and disable the default one.
$tideCoreOperation->useCustomFilesView();
// Changes the diff modules general_settings.revision_pager_limit to 16.
$tideCoreOperation->changeDiffSettings();
// Enable Tide TFA.
$tideCoreOperation->enabledTideTfa();
// Enables paragraphs_library.
$tideCoreOperation->alterParagraphsLibrary();
}
/**
* Implements hook_update_dependencies().
*/
function tide_core_update_dependencies() {
$dependencies = [];
$dependencies['tide_core'][10007] = ['bay_platform_dependencies' => 10003];
return $dependencies;
}
/**
* Increase character limit of URLs.
*/
function tide_core_update_10000() {
$config = \Drupal::configFactory()->getEditable('pathauto.settings');
$config->set('max_length', 255);
$config->save();
}
/**
* Revokes `delete any media` and `delete media` from the editor role.
*/
function tide_core_update_10001() {
$editor = Role::load('editor');
if (!$editor) {
return;
}
$permissionsToRemove = ['delete any media', 'delete media'];
$changed = FALSE;
foreach ($permissionsToRemove as $permission) {
if ($editor->hasPermission($permission)) {
$editor->revokePermission($permission);
$changed = TRUE;
}
}
if ($changed) {
$editor->save();
}
}
/**
* Grants approver role with `tide node bulk update` permission.
*/
function tide_core_update_10002() {
$approver = Role::load('approver');
$approver->grantPermission('tide node bulk update');
$approver->save();
}
/**
* Add description field to accordion.
*/
function tide_core_update_10003() {
module_load_include('inc', 'tide_core', 'includes/helpers');
$configs = [
'field.field.paragraph.accordion.field_paragraph_body' => 'field_config',
];
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_core') . '/config/install'];
// Check if field already exported to config/sync.
foreach ($configs as $config => $type) {
$config_read = _tide_read_config($config, $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage($type);
$temp = substr($config, strpos($config, '.') + 1);
$id = substr($temp, strpos($config, '.') + 1);
if ($storage->load($id) == NULL) {
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
}
$form_configs = [
'core.entity_form_display.paragraph.accordion.default',
'core.entity_view_display.paragraph.accordion.default',
'core.entity_view_display.paragraph.accordion.preview',
];
foreach ($form_configs as $form_config) {
$config = \Drupal::configFactory()->getEditable($form_config);
$config_read = _tide_read_config($form_config, $config_location, FALSE);
$config->set('dependencies', $config_read['dependencies']);
$config->set('content', $config_read['content']);
$config->set('hidden', $config_read['hidden']);
$config->save();
}
}
/**
* Approver should not have access to administer taxonomy.
*/
function tide_core_update_10004() {
$role = 'approver';
$permissions = [
'administer taxonomy',
'edit terms in topic',
'delete terms in topic',
];
if ($role) {
user_role_revoke_permissions($role, $permissions);
}
}
/**
* Run _add_default_content_category_taxonomy().
*/
function tide_core_update_10005() {
\Drupal::moduleHandler()->loadInclude('tide_core', 'inc', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_core') . '/config/install'];
$config_read = _tide_read_config('field.storage.node.field_content_category', $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
if ($storage->load('node.field_content_category') === NULL) {
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
if (\Drupal::moduleHandler()->moduleExists('term_reference_tree') === FALSE) {
\Drupal::service('module_installer')->install(['term_reference_tree']);
}
$config_read = _tide_read_config('taxonomy.vocabulary.content_category', $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
if ($storage->load('content_category') === NULL) {
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
$vocabulary_details = [
'vid' => 'content_category',
'description' => 'Categories assigned to all content to assist with filtering in content collection and search',
'name' => 'Content category',
];
_tide_core_adding_default_taxonomy(_content_category_terms(), $vocabulary_details);
}
/**
* Fixes dialog can no longer use custom data- attributes in CKEditor 5.
*/
function tide_core_update_10006() {
$config_factory = \Drupal::configFactory();
$filter_ids = [
'filter.format.admin_text',
'filter.format.rich_text',
'filter.format.summary_text',
];
foreach ($filter_ids as $filter_id) {
$filter = $config_factory->getEditable($filter_id);
$value = $filter->get('filters.filter_html.settings.allowed_html');
if ($value) {
$replaced = str_replace('<drupal-entity data-*', '<drupal-entity data-caption data-align data-entity-uuid data-embed-button data-entity-embed-display data-entity-embed-display-settings data-show-last-updated', $value);
$filter->set('filters.filter_html.settings.allowed_html', $replaced);
$filter->save();
}
}
$configs = [
'editor.editor.admin_text',
'editor.editor.rich_text',
'editor.editor.summary_text',
];
foreach ($configs as $config) {
$editable_config = $config_factory->getEditable($config);
$rows = $editable_config->get('settings.plugins.ckeditor5_sourceEditing.allowed_tags');
if ($rows) {
$replaced_value = _tide_core_replace_attribute_in_tag($rows, 'drupal-entity', 'data-*', 'data-caption data-align data-entity-type data-entity-uuid data-embed-button data-entity-embed-display data-entity-embed-display-settings data-show-last-updated');
$editable_config->set('settings.plugins.ckeditor5_sourceEditing.allowed_tags', $replaced_value);
$editable_config->save();
}
}
}
/**
* Replaces a specified attribute within a given tag in an array of strings.
*/
function _tide_core_replace_attribute_in_tag($array, $tagName, $attribute, $newAttributeString) {
foreach ($array as $key => $value) {
if (strpos($value, "<" . $tagName) !== FALSE) {
$pattern = '/' . preg_quote($attribute, '/') . '/';
$array[$key] = preg_replace($pattern, $newAttributeString, $value);
}
}
return $array;
}
/**
* Installs tide_times sensor.
*/
function tide_core_update_10007() {
\Drupal::moduleHandler()->loadInclude('tide_core', 'inc', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_core') . '/config/install'];
$config_read = _tide_read_config('monitoring.sensor_config.tide_times', $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage('monitoring_sensor_config');
$id = $storage->getIDFromConfigName('monitoring.sensor_config.tide_times', $storage->getEntityType()->getConfigPrefix());
if ($storage->load($id) == NULL) {
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
}
/**
* Enables paragraphs_library.
*/
function tide_core_update_10009() {
// Enabled paragraphs_library module.
if (!\Drupal::moduleHandler()->moduleExists('paragraphs_library')) {
/** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
$module_installer = \Drupal::service('module_installer');
$module_installer->install(['paragraphs_library']);
}
// Import extra fields.
$config_update_items = [
'paragraphs_library_item.field_department' => 'field_storage_config',
'paragraphs_library_item.field_site' => 'field_storage_config',
'paragraphs_library_item.paragraphs_library_item.field_department' => 'field_config',
'paragraphs_library_item.paragraphs_library_item.field_site' => 'field_config',
];
/** @var \Drupal\config_update\ConfigReverter $config_update */
$config_update = \Drupal::service('config_update.config_update');
foreach ($config_update_items as $name => $type) {
$config_update->import($type, $name);
}
$tideCoreOperation = new TideCoreOperation();
$tideCoreOperation->alterParagraphsLibrary();
// Update permissions.
$roles = [
'approver',
'contributor',
'editor',
'site_admin',
];
$permissions = [
'access paragraphs_library_items entity browser pages',
'create paragraph library item',
'edit paragraph library item',
];
foreach ($roles as $role_name) {
$role = Role::load($role_name);
if ($role) {
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->save();
}
}
}