1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains df_tools_workflow.module. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Drupal\Core\Entity\ContentEntityInterface; |
9
|
|
|
use Drupal\Core\Form\FormStateInterface; |
10
|
|
|
use Drupal\Core\Render\Element; |
11
|
|
|
use Drupal\Core\Url; |
12
|
|
|
use Drupal\node\Entity\Node; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements hook_modules_installed(). |
16
|
|
|
*/ |
17
|
|
|
function df_tools_workflow_modules_installed(array $modules) { |
18
|
|
|
// Don't do anything during config sync. |
19
|
|
|
if (\Drupal::isConfigSyncing()) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// Grant 'Creators' the ability to create draft content and mark it for |
24
|
|
|
// review. |
25
|
|
|
if (in_array('df_tools_user', $modules)) { |
26
|
|
|
$creator_permissions = [ |
27
|
|
|
'use moderation sidebar', |
28
|
|
|
// 'use moderation dashboard', |
29
|
|
|
'view latest version', |
30
|
|
|
'use editorial transition archive', |
31
|
|
|
'use editorial transition create_new_draft', |
32
|
|
|
'use editorial transition archived_published', |
33
|
|
|
'use editorial transition archived_draft', |
34
|
|
|
'use editorial transition review', |
35
|
|
|
]; |
36
|
|
|
user_role_grant_permissions('creator', $creator_permissions); |
37
|
|
|
|
38
|
|
|
// Grant 'Reviewers' the ability to review flagged content and publish it. |
39
|
|
|
$reviewer_permissions = [ |
40
|
|
|
'use moderation sidebar', |
41
|
|
|
// 'use moderation dashboard', |
42
|
|
|
'view latest version', |
43
|
|
|
'use editorial transition archive', |
44
|
|
|
'use editorial transition create_new_draft', |
45
|
|
|
'use editorial transition publish', |
46
|
|
|
'use editorial transition archived_published', |
47
|
|
|
'use editorial transition archived_draft', |
48
|
|
|
'use editorial transition review', |
49
|
|
|
]; |
50
|
|
|
user_role_grant_permissions('reviewer', $reviewer_permissions); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Implements hook_form_FORM_ID_alter(). |
56
|
|
|
* |
57
|
|
|
* Add a form submission handler to the content moderation module's entity |
58
|
|
|
* moderation form in order to redirect users to the latest version of the |
59
|
|
|
* content when creating a new draft. |
60
|
|
|
*/ |
61
|
|
|
function df_tools_workflow_form_content_moderation_entity_moderation_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { |
|
|
|
|
62
|
|
|
$form['#submit'][] = 'df_tools_workflow_content_moderation_entity_moderation_form_submit'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Form submission callback for content_moderation_entity_moderation_form. |
67
|
|
|
*/ |
68
|
|
|
function df_tools_workflow_content_moderation_entity_moderation_form_submit($form, FormStateInterface $form_state) { |
|
|
|
|
69
|
|
|
/** @var ContentEntityInterface $entity */ |
70
|
|
|
$entity = $form_state->get('entity'); |
71
|
|
|
|
72
|
|
|
// Retrieve the content's new state. |
73
|
|
|
$new_state = $form_state->getValue('new_state'); |
74
|
|
|
|
75
|
|
|
// When creating a new draft, redirect the user to the latest version of the |
76
|
|
|
// content. |
77
|
|
|
if ($new_state == 'draft') { |
78
|
|
|
$form_state->setRedirectUrl($entity->toUrl('latest-version')); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Implements hook_menu_local_tasks_alter(); |
84
|
|
|
*/ |
85
|
|
|
function df_tools_workflow_menu_local_tasks_alter(&$data, $route_name) { |
|
|
|
|
86
|
|
|
// Runs when cache tag config:block.block.[ActiveTheme]_local_actions is invalidated. |
87
|
|
|
if (isset($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab'])) { |
88
|
|
|
$url = $data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']['#link']['url']; |
89
|
|
|
$param = $url->getRouteParameters(); |
90
|
|
|
if (isset($param['node'])) { |
91
|
|
|
$node = Node::load($param['node']); |
92
|
|
|
$moderation_info = \Drupal::service('content_moderation.moderation_information'); |
93
|
|
|
$latest_node = $moderation_info->getLatestRevision('node', $node->id()); |
94
|
|
|
// Modify the tabs when the canonical route is the latest revision. |
95
|
|
|
if ($latest_node->getRevisionId() == $node->getRevisionId()) { |
96
|
|
|
// Highlight the 'View' tab at /node/%/latest. |
97
|
|
|
if ($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']['#active']) { |
98
|
|
|
$data['tabs'][0]['entity.node.canonical']['#active'] = TRUE; |
99
|
|
|
} |
100
|
|
|
// Remove the latest version tab. |
101
|
|
|
unset($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Implements hook_tokens(). |
109
|
|
|
*/ |
110
|
|
|
function df_tools_workflow_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) { |
111
|
|
|
$replacements = []; |
112
|
|
|
|
113
|
|
|
if ($type == 'node' && !empty($data['node'])) { |
114
|
|
|
$url_options = ['absolute' => TRUE]; |
115
|
|
|
if (isset($options['langcode'])) { |
116
|
|
|
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); |
117
|
|
|
$langcode = $options['langcode']; |
118
|
|
|
} |
119
|
|
|
else { |
120
|
|
|
$langcode = NULL; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @var \Drupal\node\NodeInterface $node */ |
124
|
|
|
$node = $data['node']; |
125
|
|
|
/** @var \Drupal\content_moderation\ModerationInformation $moderation_information */ |
126
|
|
|
$moderation_information = \Drupal::service('content_moderation.moderation_information'); |
127
|
|
|
|
128
|
|
|
/** @var \Drupal\node\NodeInterface $latest */ |
129
|
|
|
if (!($latest = $moderation_information->getLatestRevision('node', $node->id()))) { |
130
|
|
|
$latest = $node; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($langcode) { |
134
|
|
|
$latest = $latest->getTranslation($langcode); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($tokens as $name => $original) { |
138
|
|
|
switch ($name) { |
139
|
|
|
case 'latest_author': |
140
|
|
|
$replacements[$original] = $latest->getRevisionUser()->label(); |
141
|
|
|
break; |
142
|
|
|
case 'latest_url': |
143
|
|
|
if ($node->getRevisionId() !== $latest->getRevisionId()) { |
144
|
|
|
$replacements[$original] = Url::fromRoute('entity.node.latest_version', [ |
145
|
|
|
'node' => $node->id(), |
146
|
|
|
], $url_options)->toString(); |
147
|
|
|
} |
148
|
|
|
else { |
149
|
|
|
$replacements[$original] = $latest->toUrl('canonical', $url_options)->toString(); |
150
|
|
|
} |
151
|
|
|
break; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$token_service = \Drupal::token(); |
156
|
|
|
if ($author_tokens = $token_service->findWithPrefix($tokens, 'latest_author')) { |
157
|
|
|
$replacements += $token_service->generate('user', $author_tokens, ['user' => $latest->getRevisionUser()], $options, $bubbleable_metadata); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $replacements; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Implements hook_library_info_alter(). |
166
|
|
|
*/ |
167
|
|
|
function df_tools_workflow_library_info_alter(&$libraries, $extension) { |
168
|
|
|
if ($extension === 'moderation_note') { |
169
|
|
|
unset($libraries['main']['dependencies']); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Implements hook_moderation_sidebar_alter(). |
175
|
|
|
* |
176
|
|
|
* Integrate the Type Style Moderation and Moderation Sidebar modules. |
177
|
|
|
* |
178
|
|
|
* Display type style colors and icons associated with content moderation |
179
|
|
|
* transitions on their moderation sidebar buttons. |
180
|
|
|
*/ |
181
|
|
|
function df_tools_workflow_moderation_sidebar_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity) { |
182
|
|
|
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */ |
183
|
|
|
$moderation_info = \Drupal::service('content_moderation.moderation_information'); |
184
|
|
|
|
185
|
|
|
// Verify that the moderation sidebar is being displayed on a content entity |
186
|
|
|
// and that it has an associated workflow. |
187
|
|
|
if ($entity instanceof ContentEntityInterface && $workflow = $moderation_info->getWorkflowForEntity($entity)) { |
188
|
|
|
// Ensure that the sidebar's quick draft form is available. |
189
|
|
|
if (isset($build['actions']['quick_draft_form'])) { |
190
|
|
|
// Each of the quick draft form's buttons represents a moderation state |
191
|
|
|
// transition for the current workflow, such as 'create_new_draft' or |
192
|
|
|
// 'archive.' |
193
|
|
|
// Retrieve these buttons to build an array of transitions. |
194
|
|
|
$transition_ids = Element::getVisibleChildren($build['actions']['quick_draft_form']); |
195
|
|
|
|
196
|
|
|
// Only proceed if one or more transitions is available. |
197
|
|
|
if (count($transition_ids)) { |
198
|
|
|
// Type style colors and icons are stored as configuration on the |
199
|
|
|
// workflow. |
200
|
|
|
$type_plugin = $workflow->getTypePlugin(); |
201
|
|
|
$config = $type_plugin->getConfiguration(); |
202
|
|
|
|
203
|
|
|
// Loop through each of the available transitions, altering the markup |
204
|
|
|
// of each moderation sidebar button to add an icon and change its |
205
|
|
|
// background color where necessary. |
206
|
|
|
foreach ($transition_ids as $id) { |
207
|
|
|
// Retrieve the Type Style settings for the current transition. |
208
|
|
|
$settings = isset($config['transitions'][$id]['type_style']) ? $config['transitions'][$id]['type_style'] : []; |
209
|
|
|
|
210
|
|
|
if (!empty($settings)) { |
211
|
|
|
// Use the Type Style color as the button's background color. |
212
|
|
|
if (!empty($settings['color'])) { |
213
|
|
|
$build['actions']['quick_draft_form'][$id]['#attributes']['style'] = 'background-color: ' . $settings['color']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Add the Type Style icon to the button. |
217
|
|
|
if (!empty($settings['icon'])) { |
218
|
|
|
$build['actions']['quick_draft_form'][$id] = [ |
219
|
|
|
'#type' => 'container', |
220
|
|
|
$build['actions']['quick_draft_form'][$id], |
221
|
|
|
[ |
222
|
|
|
'#markup' => '<i class="material-icons">' . $settings['icon'] . '</i>' |
223
|
|
|
], |
224
|
|
|
'#attributes' => [ |
225
|
|
|
'class' => ['moderation-sidebar-button-wrapper'], |
226
|
|
|
], |
227
|
|
|
]; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.