-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalgov_workflows.module
260 lines (229 loc) · 9.62 KB
/
localgov_workflows.module
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
<?php
/**
* @file
* Localgov Workflow module hooks.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\localgov_roles\RolesHelper;
use Drupal\localgov_workflows\RequireLogMessage;
use Drupal\node\NodeTypeInterface;
use Drupal\workflows\Entity\Workflow;
/**
* Implements hook_modules_installed().
*/
function localgov_workflows_modules_installed($modules, $is_syncing) {
// Fix the core moderated content view.
if (in_array('localgov_workflows', $modules, TRUE) && !$is_syncing) {
// Don't change things if the core editorial workflow is enabled.
if (!is_null(\Drupal::entityTypeManager()->getStorage('workflow')->load('editorial'))) {
return;
}
$moderated_content_view = \Drupal::entityTypeManager()
->getStorage('view')
->load('moderated_content');
if ($moderated_content_view) {
// Fix filters to match editorial workflow.
$display = $moderated_content_view->get('display');
$filters = $display['default']['display_options']['filters'];
$filters['moderation_state']['value'] = [
'localgov_editorial-draft' => 'localgov_editorial-draft',
'localgov_editorial-review' => 'localgov_editorial-review',
'localgov_editorial-archived' => 'localgov_editorial-archived',
];
$filters['moderation_state_1']['value'] = [
'localgov_editorial-published' => 'localgov_editorial-published',
];
$display['default']['display_options']['filters'] = $filters;
// Update no results text.
$display['default']['display_options']['empty']['area_text_custom']['content'] = 'No unpublished content available. Pages in Draft, Review and Archived states appear here.';
$moderated_content_view->set('display', $display);
$moderated_content_view->save();
}
}
}
/**
* Implements hook_localgov_roles_default().
*/
function localgov_workflows_localgov_roles_default(): array {
return [
RolesHelper::EDITOR_ROLE => [
'use localgov_editorial transition approve',
'use localgov_editorial transition archive',
'use localgov_editorial transition archived_draft',
'use localgov_editorial transition archived_published',
'use localgov_editorial transition create_new_draft',
'use localgov_editorial transition publish',
'use localgov_editorial transition reject',
'use localgov_editorial transition submit_for_review',
'view all scheduled transitions',
'view any unpublished content',
'view latest version',
],
RolesHelper::AUTHOR_ROLE => [
'use localgov_editorial transition archive',
'use localgov_editorial transition create_new_draft',
'use localgov_editorial transition reject',
'use localgov_editorial transition submit_for_review',
'use localgov_editorial transition publish',
'view all scheduled transitions',
'view any unpublished content',
'view latest version',
],
RolesHelper::CONTRIBUTOR_ROLE => [
'use localgov_editorial transition create_new_draft',
'use localgov_editorial transition reject',
'use localgov_editorial transition submit_for_review',
'view all scheduled transitions',
'view any unpublished content',
'view latest version',
'use localgov_editorial transition create_new_draft',
'use localgov_editorial transition reject',
'use localgov_editorial transition submit_for_review',
'view any unpublished content',
'view latest version',
],
];
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function localgov_workflows_node_type_insert(NodeTypeInterface $node_type) {
// Add workflow to new localgov_ node bundle with no other workflow.
if (strpos($node_type->id(), 'localgov_') === 0) {
$types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('node');
if (empty($types['node']['workflow'])) {
// The localgov_editorial workflow may not exist during site install.
if ($editorial = Workflow::load('localgov_editorial')) {
$type = $editorial->getTypePlugin();
$type->addEntityTypeAndBundle('node', $node_type->id());
$editorial->save();
}
}
}
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function localgov_workflows_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface &$cacheability) {
// Disabled the default 'content moderation` tab on admin content pages as
// this expects the default 'editorial' workflow is enabled and doesn't work
// with other workflows without altering the content moderation view.
$admin_content_routes = [
'system.admin_content',
'view.localgov_approvals_dashboard.approvals_dashboard',
];
if (in_array($route_name, $admin_content_routes, TRUE)) {
// Check if there are more workflows enabled than localgov_editorial.
$workflows = \Drupal::entityQuery('workflow')->accessCheck(TRUE)->execute();
if (in_array('localgov_editorial', $workflows, TRUE) && count($workflows) == 1) {
// Hide the moderated content tab.
for ($i = 0; $i < count($data['tabs']); $i++) {
foreach ($data['tabs'][$i] as $key => $tab) {
if ($key == 'content_moderation.moderated_content') {
$data['tabs'][$i][$key]['#access'] = AccessResult::neutral();
}
}
}
}
}
// Rename 'Scheduled transitions' tab to 'Scheduling' and increase its weight.
if (isset($data['tabs'][0]['entity.scheduled_transition.collection'])) {
$data['tabs'][0]['entity.scheduled_transition.collection']['#link']['title'] = t('Scheduling');
$data['tabs'][0]['entity.scheduled_transition.collection']['#weight'] = 70;
}
if (isset($data['tabs'][0]['scheduled_transitions.tasks:node.scheduled_transitions']['#link']['title'])) {
$title = $data['tabs'][0]['scheduled_transitions.tasks:node.scheduled_transitions']['#link']['title'];
$arguments = $title->getArguments();
$arguments['@title'] = 'Scheduling';
$data['tabs'][0]['scheduled_transitions.tasks:node.scheduled_transitions']['#link']['title'] = t($title->getUntranslatedString(), $arguments); // phpcs:ignore.
}
// Rename 'Moderated content' tab to 'Unpublished'.
if (isset($data['tabs'][1]['content_moderation.workflows:content_moderation.moderated_content'])) {
$data['tabs'][1]['content_moderation.workflows:content_moderation.moderated_content']['#link']['title'] = t('Unpublished');
$data['tabs'][1]['content_moderation.workflows:content_moderation.moderated_content']['#weight'] = 10;
}
// Add cache dependency on workflow.
$cacheability->addCacheTags(['config:workflow_list']);
}
/**
* Implements hook_menu_local_actions_alter().
*/
function localgov_workflows_menu_local_actions_alter(&$local_actions) {
// Rename 'Add scheduled transitions' action to 'Add schedule'.
foreach ($local_actions as $key => $action) {
if (preg_match('/^scheduled_transitions\.actions:.*\.add_scheduled_transition$/', $key)) {
$local_actions[$key]['title'] = t('Add schedule');
}
}
}
/**
* Implements hook_preprocess_html().
*/
function localgov_workflows_preprocess_html(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name === 'entity.scheduled_transition.collection') {
$variables['head_title']['title'] = t('Scheduling');
}
elseif ($route_name === 'entity.scheduled_transition.reschedule_form') {
$variables['head_title']['title'] = t('Reschedule');
}
elseif ($route_name === 'content_moderation.admin_moderated_content') {
$variables['head_title']['title'] = t('Unpublished');
}
}
/**
* Implements hook_preprocess_page_title().
*/
function localgov_workflows_preprocess_page_title(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name === 'entity.scheduled_transition.collection') {
$variables['title'] = t('Scheduling');
}
elseif ($route_name === 'content_moderation.admin_moderated_content') {
$variables['title'] = t('Unpublished');
}
}
/**
* Implements hook_form_alter().
*/
function localgov_workflows_form_alter(&$form, FormStateInterface $form_state, string $form_id) {
// Rename 'Scheduled transition' form items to 'Schedule'.
if (str_ends_with($form_id, 'scheduled_transitions_add_form_form')) {
$form['actions']['submit']['#value'] = t('Schedule');
}
// Rename 'Reschedule transition' form items to 'Reschedule'.
if ($form_id == 'scheduled_transition_reschedule_form') {
$form['actions']['submit']['#value'] = t('Reschedule');
}
}
/**
* Implements hook_menu_discovered_alter().
*/
function localgov_workflows_menu_links_discovered_alter(array &$links) {
// Rename 'Scheduled transitions' menu item to 'Scheduling'.
if (isset($links['entity.scheduled_transition.collection'])) {
$links['entity.scheduled_transition.collection']['title'] = t('Scheduling');
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function localgov_workflows_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
return \Drupal::classResolver(RequireLogMessage::class)
->alterNodeTypeForm($form, $form_state);
}
/**
* Implements hook_module_implements_alter().
*/
function localgov_workflows_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter' && isset($implementations['localgov_workflows'])) {
// field_ui was altering the node_type form before us, copying and hiding
// the submit button, and thus handlers. As some other module could do
// the same we'll just get in early.
$group = $implementations['localgov_workflows'];
unset($implementations['localgov_workflows']);
$implementations = ['localgov_workflows' => $group] + $implementations;
}
}