forked from localgovdrupal/localgov_workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localgov_workflows.module
129 lines (118 loc) · 4.64 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
<?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_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'])) {
$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)) {
// Check if there are more workflows enabled than localgov_editorial.
$workflows = \Drupal::entityQuery('workflow')->accessCheck(TRUE)->execute();
if (in_array('localgov_editorial', $workflows) && 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();
}
}
}
}
}
// Add cache dependency on workflow.
$cacheability->addCacheTags(['config:workflow_list']);
}
/**
* Imlements 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;
}
}