-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucb_subtonode.install
More file actions
352 lines (312 loc) · 9.33 KB
/
Copy pathucb_subtonode.install
File metadata and controls
352 lines (312 loc) · 9.33 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
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
<?php
/**
* @file
* Install and update hooks for ucb_subtonode.
*/
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\webform\Entity\Webform;
/**
* Returns the default bulletin taxonomy terms.
*
* @return array
* Vocabulary machine names mapped to term labels.
*/
function _ucb_subtonode_default_terms(): array {
return [
'audience' => [
'Community',
'Faculty/Staff',
'Graduate Students',
'Undergraduate Students',
],
'category' => [
'Academics',
'Administration',
'Announcements',
'Benefits',
'Cone Zone',
'Deadlines',
'Entrepreneurship',
'Family',
'Getting Involved',
'Health & Wellness',
'Housing & Dining',
'Jobs & Internships',
'Leadership Opportunities',
'Meetings',
'Nominations',
'Research Opportunities',
'Safety',
'Scholarships, Fellowships & Grants',
'Sports & Recreation',
'Surveys & Quizzes',
'Sustainability',
'Technology',
'Training & Workshops',
'Transportation',
],
];
}
/**
* Ensures default audience and category terms exist.
*/
function _ucb_subtonode_ensure_default_terms(): void {
$term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
foreach (_ucb_subtonode_default_terms() as $vid => $terms) {
$existing = $term_storage->loadByProperties(['vid' => $vid]);
$existing_names = [];
foreach ($existing as $term) {
$existing_names[$term->label()] = TRUE;
}
foreach ($terms as $weight => $name) {
if (!isset($existing_names[$name])) {
Term::create([
'vid' => $vid,
'name' => $name,
'weight' => $weight,
])->save();
}
}
}
}
/**
* Replaces site_owner with site_manager in role lists and remember_roles.
*
* site_owner is symbolic and must not convey permissions.
*
* @param array $data
* Config data to update in place.
*/
function _ucb_subtonode_replace_site_owner_roles(array &$data): void {
foreach ($data as $key => &$value) {
if (!is_array($value)) {
continue;
}
if ($key === 'remember_roles' && array_key_exists('site_owner', $value)) {
unset($value['site_owner']);
if (!array_key_exists('site_manager', $value)) {
$value['site_manager'] = '0';
}
}
elseif ($key === 'roles' && in_array('site_owner', $value, TRUE)) {
$value = array_values(array_unique(array_map(
static fn (string $role): string => $role === 'site_owner' ? 'site_manager' : $role,
$value,
)));
}
else {
_ucb_subtonode_replace_site_owner_roles($value);
}
}
}
/**
* Ensures post_bulletin webform access requires authenticated users to submit.
*/
function _ucb_subtonode_ensure_webform_access(): void {
$webform = Webform::load('post_bulletin');
if (!$webform) {
return;
}
$access = $webform->get('access');
$authenticated_roles = ['authenticated'];
foreach (['create', 'view_own', 'update_own'] as $operation) {
$access[$operation]['roles'] = $authenticated_roles;
}
$editorial_roles = ['developer', 'architect', 'site_manager'];
foreach (['view_any', 'update_any', 'delete_own'] as $operation) {
$access[$operation]['roles'] = $editorial_roles;
}
_ucb_subtonode_replace_site_owner_roles($access);
$elements = $webform->getElementsDecoded();
_ucb_subtonode_replace_site_owner_roles($elements);
$webform->set('access', $access);
$webform->setElements($elements);
$webform->save();
}
/**
* Ensures the post_bulletin webform includes a media library image element.
*/
function _ucb_subtonode_ensure_webform_media_element(): void {
$webform = Webform::load('post_bulletin');
if (!$webform) {
return;
}
$elements = $webform->getElementsDecoded();
$elements['image'] = [
'#type' => 'media_library',
'#title' => 'Image',
'#bundles' => [
'image' => 'image',
],
];
// Keep image near the top of the form, after body.
$ordered = [];
foreach (['title', 'body', 'image'] as $key) {
if (isset($elements[$key])) {
$ordered[$key] = $elements[$key];
unset($elements[$key]);
}
}
$elements = $ordered + $elements;
$webform->setElements($elements);
$webform->save();
}
/**
* Returns bulletin node permissions granted to editorial roles.
*
* @return string[]
* Permission machine names.
*/
function _ucb_subtonode_bulletin_node_permissions(): array {
return [
'create bulletin content',
'edit any bulletin content',
'edit own bulletin content',
'delete any bulletin content',
'delete own bulletin content',
'delete bulletin revisions',
'revert bulletin revisions',
'view bulletin revisions',
];
}
/**
* Grants bulletin and webform submission permissions to site roles.
*
* @return array
* Role IDs mapped to one of: updated, unchanged, missing.
*/
function _ucb_subtonode_grant_permissions(): array {
$bulletin_permissions = _ucb_subtonode_bulletin_node_permissions();
$create_from_submission = ['create bulletin from webform submission'];
$role_permissions = [
'developer' => array_merge($bulletin_permissions, $create_from_submission),
'architect' => array_merge($bulletin_permissions, $create_from_submission),
'site_manager' => array_merge($bulletin_permissions, $create_from_submission),
'content_editor' => array_merge($bulletin_permissions, $create_from_submission),
];
$results = [];
$role_storage = \Drupal::entityTypeManager()->getStorage('user_role');
foreach ($role_permissions as $role_id => $permissions) {
$role = $role_storage->load($role_id);
if (!$role) {
$results[$role_id] = 'missing';
continue;
}
$changed = FALSE;
foreach ($permissions as $permission) {
if (!$role->hasPermission($permission)) {
$role->grantPermission($permission);
$changed = TRUE;
}
}
if ($changed) {
$role->save();
$results[$role_id] = 'updated';
}
else {
$results[$role_id] = 'unchanged';
}
}
return $results;
}
/**
* Installs bulletin board views that are not yet present.
*/
function _ucb_subtonode_install_views(): void {
$module_path = \Drupal::service('extension.list.module')->getPath('ucb_subtonode');
$installer = \Drupal::service('config.installer');
$install_storage = new FileStorage($module_path . '/config/install', StorageInterface::DEFAULT_COLLECTION);
$installer->installOptionalConfig($install_storage);
$optional_path = $module_path . '/config/optional';
if (is_dir($optional_path)) {
$optional_storage = new FileStorage($optional_path, StorageInterface::DEFAULT_COLLECTION);
$installer->installOptionalConfig($optional_storage);
}
}
/**
* Implements hook_install().
*/
function ucb_subtonode_install(): void {
_ucb_subtonode_ensure_default_terms();
_ucb_subtonode_ensure_webform_access();
_ucb_subtonode_ensure_webform_media_element();
_ucb_subtonode_grant_permissions();
}
/**
* Ensure bulletin taxonomy terms and webform image field are present.
*/
function ucb_subtonode_update_9001(): void {
_ucb_subtonode_ensure_default_terms();
_ucb_subtonode_ensure_webform_media_element();
}
/**
* Switch the bulletin webform image element to the media library widget.
*/
function ucb_subtonode_update_9002(): void {
_ucb_subtonode_ensure_webform_media_element();
}
/**
* Migrate bulletin field_photo from image files to media library references.
*/
function ucb_subtonode_update_9003(): void {
ucb_subtonode_migrate_photo_field_to_media();
}
/**
* Import the bulletin full view display configuration.
*/
function ucb_subtonode_update_9004(): void {
$config_path = \Drupal::service('extension.list.module')->getPath('ucb_subtonode') . '/config/install/core.entity_view_display.node.bulletin.full.yml';
if (!file_exists($config_path)) {
return;
}
$data = Yaml::decode(file_get_contents($config_path));
\Drupal::configFactory()->getEditable('core.entity_view_display.node.bulletin.full')->setData($data)->save(TRUE);
}
/**
* Install bulletin board views on sites that enabled the module earlier.
*/
function ucb_subtonode_update_9005(): void {
_ucb_subtonode_install_views();
}
/**
* Grant bulletin content and webform submission permissions to site roles.
*/
function ucb_subtonode_update_9006(): void {
_ucb_subtonode_ensure_webform_access();
_ucb_subtonode_grant_permissions();
}
/**
* Replace site_owner with site_manager in bulletin views and webform access.
*/
function ucb_subtonode_update_9007(): void {
$view_ids = [
'views.view.buff_bulletin_board',
'views.view.buff_bulletin_board_data',
'views.view.buff_bulletin_board_home',
];
foreach ($view_ids as $config_name) {
$config = \Drupal::configFactory()->getEditable($config_name);
if ($config->isNew()) {
continue;
}
$data = $config->getRawData();
_ucb_subtonode_replace_site_owner_roles($data);
$config->setData($data)->save(TRUE);
}
_ucb_subtonode_ensure_webform_access();
}
/**
* Grant update_any and delete_own webform submission access to editorial roles.
*/
function ucb_subtonode_update_9008(): void {
_ucb_subtonode_ensure_webform_access();
}
/**
* Grant create-bulletin-from-submission permission to content_editor.
*/
function ucb_subtonode_update_9009(): void {
_ucb_subtonode_grant_permissions();
}