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_alter(). |
56
|
|
|
* |
57
|
|
|
* We auto-populate and hide the Revision log message on node/add/* forms. |
58
|
|
|
*/ |
59
|
|
|
function df_tools_workflow_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { |
|
|
|
|
60
|
|
|
if (\Drupal::routeMatch()->getRouteName() == 'node.add') { |
61
|
|
|
$replace = [ |
62
|
|
|
'@user' => \Drupal::currentUser()->getDisplayName(), |
63
|
|
|
'@date' => date("F jS, Y") |
64
|
|
|
]; |
65
|
|
|
$form['revision_log']['widget'][0]['value']['#default_value'] = t('Created by @user on @date.', $replace); |
66
|
|
|
$form['revision_log']['widget']['#access'] = FALSE; |
67
|
|
|
} |
68
|
|
|
$form['#attached']['library'][] = 'df_tools_workflow/df_tools_workflow'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Implements hook_form_FORM_ID_alter(). |
73
|
|
|
* |
74
|
|
|
* Add a form submission handler to the content moderation module's entity |
75
|
|
|
* moderation form in order to redirect users to the latest version of the |
76
|
|
|
* content when creating a new draft. |
77
|
|
|
*/ |
78
|
|
|
function df_tools_workflow_form_content_moderation_entity_moderation_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { |
|
|
|
|
79
|
|
|
$form['#submit'][] = 'df_tools_workflow_content_moderation_entity_moderation_form_submit'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Form submission callback for content_moderation_entity_moderation_form. |
84
|
|
|
*/ |
85
|
|
|
function df_tools_workflow_content_moderation_entity_moderation_form_submit($form, FormStateInterface $form_state) { |
|
|
|
|
86
|
|
|
/** @var ContentEntityInterface $entity */ |
87
|
|
|
$entity = $form_state->get('entity'); |
88
|
|
|
|
89
|
|
|
// Retrieve the content's new state. |
90
|
|
|
$new_state = $form_state->getValue('new_state'); |
91
|
|
|
|
92
|
|
|
// When creating a new draft, redirect the user to the latest version of the |
93
|
|
|
// content. |
94
|
|
|
if ($new_state == 'draft') { |
95
|
|
|
$form_state->setRedirectUrl($entity->toUrl('latest-version')); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Implements hook_menu_local_tasks_alter(); |
101
|
|
|
*/ |
102
|
|
|
function df_tools_workflow_menu_local_tasks_alter(&$data, $route_name) { |
|
|
|
|
103
|
|
|
// Runs when cache tag config:block.block.[ActiveTheme]_local_actions is invalidated. |
104
|
|
|
if (isset($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab'])) { |
105
|
|
|
$url = $data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']['#link']['url']; |
106
|
|
|
$param = $url->getRouteParameters(); |
107
|
|
|
if (isset($param['node'])) { |
108
|
|
|
$node = Node::load($param['node']); |
109
|
|
|
$moderation_info = \Drupal::service('content_moderation.moderation_information'); |
110
|
|
|
$latest_node = $moderation_info->getLatestRevision('node', $node->id()); |
111
|
|
|
// Modify the tabs when the canonical route is the latest revision. |
112
|
|
|
if ($latest_node->getRevisionId() == $node->getRevisionId()) { |
113
|
|
|
// Highlight the 'View' tab at /node/%/latest. |
114
|
|
|
if ($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']['#active']) { |
115
|
|
|
$data['tabs'][0]['entity.node.canonical']['#active'] = TRUE; |
116
|
|
|
} |
117
|
|
|
// Remove the latest version tab. |
118
|
|
|
unset($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Implements hook_tokens(). |
126
|
|
|
*/ |
127
|
|
|
function df_tools_workflow_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) { |
128
|
|
|
$replacements = []; |
129
|
|
|
|
130
|
|
|
if ($type == 'node' && !empty($data['node'])) { |
131
|
|
|
/** @var \Drupal\node\NodeInterface $node */ |
132
|
|
|
$node = $data['node']; |
133
|
|
|
$url_options = ['absolute' => TRUE]; |
134
|
|
|
if (isset($options['langcode']) && $node->hasTranslation($options['langcode'])) { |
135
|
|
|
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); |
136
|
|
|
$langcode = $options['langcode']; |
137
|
|
|
} |
138
|
|
|
else { |
139
|
|
|
$langcode = NULL; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @var \Drupal\content_moderation\ModerationInformation $moderation_information */ |
143
|
|
|
$moderation_information = \Drupal::service('content_moderation.moderation_information'); |
144
|
|
|
|
145
|
|
|
/** @var \Drupal\node\NodeInterface $latest */ |
146
|
|
|
if (!($latest = $moderation_information->getLatestRevision('node', $node->id()))) { |
147
|
|
|
$latest = $node; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($langcode) { |
151
|
|
|
$latest = $latest->getTranslation($langcode); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
foreach ($tokens as $name => $original) { |
155
|
|
|
switch ($name) { |
156
|
|
|
case 'latest_author': |
157
|
|
|
$replacements[$original] = $latest->getRevisionUser()->label(); |
158
|
|
|
break; |
159
|
|
|
case 'latest_url': |
160
|
|
|
if ($node->getRevisionId() !== $latest->getRevisionId()) { |
161
|
|
|
$replacements[$original] = Url::fromRoute('entity.node.latest_version', [ |
162
|
|
|
'node' => $node->id(), |
163
|
|
|
], $url_options)->toString(); |
164
|
|
|
} |
165
|
|
|
else { |
166
|
|
|
$replacements[$original] = $latest->toUrl('canonical', $url_options)->toString(); |
167
|
|
|
} |
168
|
|
|
break; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$token_service = \Drupal::token(); |
173
|
|
|
if ($author_tokens = $token_service->findWithPrefix($tokens, 'latest_author')) { |
174
|
|
|
$replacements += $token_service->generate('user', $author_tokens, ['user' => $latest->getRevisionUser()], $options, $bubbleable_metadata); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $replacements; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Implements hook_library_info_alter(). |
183
|
|
|
*/ |
184
|
|
|
function df_tools_workflow_library_info_alter(&$libraries, $extension) { |
185
|
|
|
if ($extension === 'moderation_note') { |
186
|
|
|
unset($libraries['main']['dependencies']); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.