-
Notifications
You must be signed in to change notification settings - Fork 8
/
localgov_alert_banner.install
249 lines (222 loc) · 7.84 KB
/
localgov_alert_banner.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
<?php
/**
* @file
* LocalGov Alert Banner install file.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_install().
*/
function localgov_alert_banner_install($is_syncing) {
// Don't run this if syncing.
if ($is_syncing) {
return;
}
// Configure scheduled transitions if enabled.
if (\Drupal::moduleHandler()->moduleExists('scheduled_transitions')) {
localgov_alert_banner_configure_scheduled_transitions();
}
localgov_alert_banner_set_default_permissions();
}
/**
* Update alert banner entity definition to include the token on the entity.
*/
function localgov_alert_banner_update_8801() {
$field_storage_definition = BaseFieldDefinition::create('string')
->setSetting('max_length', 64)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('token', 'localgov_alert_banner', 'localgov_alert_banner', $field_storage_definition);
}
/**
* Add visibility field to existing alert banners.
*/
function localgov_alert_banner_update_8901() {
// Enable dependent condition_field module.
\Drupal::service('module_installer')->install(['condition_field']);
$config_directory = new FileStorage(__DIR__ . '/config/install');
// Add visibility field storage.
$field_storage = $config_directory->read('field.storage.localgov_alert_banner.visibility');
if ($field_storage && !FieldStorageConfig::loadByName('localgov_alert_banner', 'visibility')) {
FieldStorageConfig::create($field_storage)->save();
}
// Fetch all configured localgov_alert_banner bundles.
$alert_banner_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('localgov_alert_banner');
foreach ($alert_banner_bundles as $bundle => $info) {
// Add visibility field to bundle.
$field_record = $config_directory->read('field.field.localgov_alert_banner.localgov_alert_banner.visibility');
if ($field_record && !FieldConfig::loadByName('localgov_alert_banner', $bundle, 'visibility')) {
$field_record['bundle'] = $bundle;
FieldConfig::create($field_record)->save();
}
// Add visibility field to the bundles entity form.
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('localgov_alert_banner.' . $bundle . '.default');
if ($form_display) {
$form_display->setComponent('visibility', [
'region' => 'content',
'settings' => [],
'third_party_settings' => [],
'type' => 'condition_field_default',
'weight' => 5,
]);
$form_display->save();
}
}
}
/**
* Add workflow and configure scheduled transitions if enabled.
*/
function localgov_alert_banner_update_9001() {
// Add localgov_alert_banner workflow config.
$workflows_workflow_localgov_alert_banner = <<<EOY
langcode: en
status: true
dependencies:
config:
- localgov_alert_banner.localgov_alert_banner_type.localgov_alert_banner
module:
- content_moderation
id: localgov_alert_banners
label: 'Alert banners'
type: content_moderation
type_settings:
states:
draft:
published: false
default_revision: false
label: Draft
weight: 0
published:
label: Published
published: true
default_revision: true
weight: 1
unpublished:
label: Unpublished
published: false
default_revision: true
weight: 2
transitions:
create_new_draft:
label: 'Create New Draft'
to: draft
weight: 0
from:
- draft
- published
- unpublished
publish:
label: Publish
to: published
weight: 1
from:
- draft
- published
- unpublished
unpublish:
label: Unpublish
to: unpublished
weight: 2
from:
- draft
- published
- unpublished
entity_types:
localgov_alert_banner:
- localgov_alert_banner
default_moderation_state: unpublished
EOY;
$workflow = \Drupal::service('config.factory')->getEditable('workflows.workflow.localgov_alert_banners');
assert($workflow instanceof Config);
if ($workflow->isNew()) {
$workflow->setData(Yaml::parse($workflows_workflow_localgov_alert_banner));
$workflow->save();
}
// Hide content moderation field on alert banner display.
$alert_banner_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('localgov_alert_banner');
foreach ($alert_banner_bundles as $bundle => $info) {
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('localgov_alert_banner.' . $bundle . '.default');
if ($form_display) {
$form_display->setComponent('content_moderation_control', [
'region' => 'hidden',
]);
$form_display->save();
}
}
// Check Scheduled Transitions module is enabled.
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('scheduled_transitions')) {
// Configure scheduled transitions for alert banners.
$scheduled_transitions_config = \Drupal::service('config.factory')->getEditable('scheduled_transitions.settings');
$bundles = [];
foreach ($alert_banner_bundles as $bundle => $info) {
$bundles[] = [
'entity_type' => 'localgov_alert_banner',
'bundle' => $bundle,
];
}
$scheduled_transitions_config->set('bundles', $bundles);
$scheduled_transitions_config->save();
Cache::invalidateTags([
'scheduled_transition_settings',
'config:scheduled_transitions.settings',
]);
// Add scheduling permissions to the emergency publisher user.
$permissions = [];
foreach ($alert_banner_bundles as $bundle => $info) {
$permissions = array_merge($permissions, [
'add scheduled transitions localgov_alert_banner ' . $bundle,
'reschedule scheduled transitions localgov_alert_banner ' . $bundle,
'view scheduled transitions localgov_alert_banner ' . $bundle,
]);
}
user_role_grant_permissions('emergency_publisher', $permissions);
}
}
/**
* Add view alert banner permission to anonymous and authenticated users.
*/
function localgov_alert_banner_update_9002() {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view all localgov alert banner entities']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view all localgov alert banner entities']);
// Also grant permissions on individual types.
$alert_banner_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('localgov_alert_banner');
foreach ($alert_banner_bundles as $bundle => $info) {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view localgov alert banner ' . $bundle . ' entities']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view localgov alert banner ' . $bundle . ' entities']);
}
}
/**
* Assign additional permissions to Emergency publisher role.
*
* Give emergency publisher permissions to access the alert banner manage screen
* from the toolbar.
*/
function localgov_alert_banner_update_10002() {
$module_handler = \Drupal::service('module_handler');
$perms[] = 'view the administration theme';
if ($module_handler->moduleExists('node')) {
$perms[] = 'access content overview';
}
if ($module_handler->moduleExists('toolbar')) {
$perms[] = 'access toolbar';
}
// Add and revoke permissions if emergency publisher exists.
if (Role::load('emergency_publisher') instanceof RoleInterface) {
user_role_grant_permissions('emergency_publisher', $perms);
user_role_revoke_permissions('emergency_publisher', ['access administration pages']);
}
}