forked from localgovdrupal/localgov_workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localgov_workflows.install
159 lines (140 loc) · 5.43 KB
/
localgov_workflows.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
<?php
/**
* @file
* Install, update and uninstall functions for the LocalGov Workflow module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Site\Settings;
use Drupal\localgov_roles\RolesHelper;
use Drupal\scheduled_transitions\Form\ScheduledTransitionsSettingsForm;
use Drupal\user\Entity\Role;
use Drupal\workflows\Entity\Workflow;
/**
* Implements hook_install().
*/
function localgov_workflows_install($is_syncing) {
// Configure workflow for localgov_ node bundles with no other workflow.
$editorial = Workflow::load('localgov_editorial');
if (!$editorial) {
$config_key = 'workflows.workflow.localgov_editorial';
if ($is_syncing) {
// If syncing config try to load the workflow from exported config.
$config_source = new FileStorage(Settings::get('config_sync_directory'));
$editorial_config = $config_source->read($config_key);
}
if (!$is_syncing || !$editorial_config) {
// If workflow doesn't already exist load it from this module.
$config_file_path = \Drupal::service('extension.path.resolver')->getPath('module', 'localgov_workflows') . '/config/install';
$config_source = new FileStorage($config_file_path);
$editorial_config = $config_source->read($config_key);
}
\Drupal::entityTypeManager()->getStorage('workflow')
->create($editorial_config)
->save();
$editorial = Workflow::load('localgov_editorial');
}
$workflow_type = $editorial->getTypePlugin();
$node_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('node');
$changed = FALSE;
foreach ($node_types as $type_name => $node_type) {
if (strpos($type_name, 'localgov_') === 0 && empty($node_type['workflow'])) {
// Add workflow.
$workflow_type->addEntityTypeAndBundle('node', $type_name);
$changed = TRUE;
}
}
if ($changed) {
$editorial->save();
}
// Enable scheduled transitions for all node bundles.
$scheduled_transitions_config = \Drupal::service('config.factory')->getEditable('scheduled_transitions.settings');
$bundles = $scheduled_transitions_config->get('bundles') ?? [];
foreach ($node_types as $bundle => $node_type) {
if (empty($bundles) || !in_array($bundle, array_column($bundles, 'bundle'))) {
$bundles[] = [
'entity_type' => 'node',
'bundle' => $bundle,
];
}
}
$scheduled_transitions_config->set('bundles', $bundles);
$scheduled_transitions_config->save();
Cache::invalidateTags([
ScheduledTransitionsSettingsForm::SETTINGS_TAG,
'config:scheduled_transitions.settings',
]);
// Ensure contributor role exists.
localgov_workflows_update_9001();
// Use the admin theme when checking diffs.
if (\Drupal::moduleHandler()->moduleExists('diff')) {
$config = \Drupal::configFactory()->getEditable('diff.settings');
$settings = $config->get('general_settings');
$settings['visual_inline_theme'] = 'admin';
$config->set('general_settings', $settings);
$config->save();
}
// Reload user permissions defined in hook_localgov_roles_default().
$roles = \Drupal::service('module_handler')->invokeAll('localgov_roles_default');
foreach ($roles as $role => $permissions) {
user_role_grant_permissions($role, $permissions);
}
}
/**
* Install the contributor role if it doesn't exist.
*/
function localgov_workflows_update_9001() {
// Workflow assumes that editor, author and contributor roles exists. The
// first two have been included in the LocalGov Roles module from the
// beginning, but the contributor role is new and so should be added if it's
// not present.
if (!Role::load(RolesHelper::CONTRIBUTOR_ROLE)) {
$config_key = 'user.role.' . RolesHelper::CONTRIBUTOR_ROLE;
$config_file_path = \Drupal::service('extension.path.resolver')->getPath('module', 'localgov_roles') . '/config/install';
$source = new FileStorage($config_file_path);
$contributor_config = $source->read($config_key);
\Drupal::entityTypeManager()->getStorage('user_role')
->create($contributor_config)
->save();
}
}
/**
* Ensure approvals dashboard view is removed when uninstalling the module.
*/
function localgov_workflows_update_9002() {
// Add enforced dependency to localgov_workflows approvals dashboard view.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('views.view.localgov_approvals_dashboard');
$dependencies = $config->get('dependencies');
$updated = FALSE;
if (isset($dependencies['enforced']['module']) && !in_array('localgov_workflows', $dependencies['enforced']['module'])) {
$dependencies['enforced']['module'][] = 'localgov_workflows';
$updated = TRUE;
}
elseif (!isset($dependencies['enforced'])) {
$dependencies['enforced'] = [
'module' => [
'localgov_workflows',
],
];
$updated = TRUE;
}
if ($updated) {
$config->set('dependencies', $dependencies);
$config->save();
}
}
/**
* Enable Diff module and configure it to use the admin theme.
*/
function localgov_workflows_update_9003() {
// Enable and configure the Diff module if it's not already enabled.
if (!\Drupal::moduleHandler()->moduleExists('diff')) {
\Drupal::service('module_installer')->install(['diff']);
$config = \Drupal::configFactory()->getEditable('diff.settings');
$settings = $config->get('general_settings');
$settings['visual_inline_theme'] = 'admin';
$config->set('general_settings', $settings);
$config->save();
}
}