This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler_content_moderation_integration.module
305 lines (266 loc) · 12 KB
/
scheduler_content_moderation_integration.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* @file
* Scheduler Content Moderation Integration.
*
* This sub-module provides extended options widget populated with default
* revision states to allow publishing and un-publishing of nodes which are
* moderated.
*
* @see https://www.drupal.org/project/scheduler/issues/2798689
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workflows\Transition;
/**
* Implements hook_entity_access().
*
* Deny access to users not having access to scheduled transitions, similar to
* what is done in content_moderation_entity_access() by checking for valid
* transitions.
*/
function scheduler_content_moderation_integration_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
$moderation_info = Drupal::service('content_moderation.moderation_information');
if ($operation === 'update' && $moderation_info->isModeratedEntity($entity) && $entity->moderation_state
&& ($entity->publish_on->value || $entity->unpublish_on->value)) {
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $moderation_info->getWorkflowForEntity($entity);
$current_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
/** @var \Drupal\content_moderation\StateTransitionValidation $transition_validation */
$transition_validation = \Drupal::service('content_moderation.state_transition_validation');
foreach ([$entity->publish_state, $entity->unpublish_state] as $state) {
try {
$scheduled_state = $workflow->getTypePlugin()->getState($state->value);
if (!$transition_validation->isTransitionValid($workflow, $current_state, $scheduled_state, $account, $entity)) {
return AccessResult::forbidden('Scheduled transition is not valid for the given account.');
}
}
catch (\InvalidArgumentException $exception) {
// Just move on when there is no valid state.
}
}
}
return AccessResult::neutral();
}
/**
* Implements hook_entity_base_field_info().
*/
function scheduler_content_moderation_integration_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'node') {
$fields['publish_state'] = BaseFieldDefinition::create('list_string')
->setSetting('allowed_values_function', '_scheduler_content_moderation_integration_states_values')
->setLabel(t('Publish state'))
->setDisplayOptions('view', [
'label' => 'hidden',
'region' => 'hidden',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'scheduler_moderation',
'weight' => 30,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', FALSE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->addConstraint('SchedulerPublishState')
->addConstraint('SchedulerModerationTransitionAccess');
$fields['unpublish_state'] = BaseFieldDefinition::create('list_string')
->setSetting('allowed_values_function', '_scheduler_content_moderation_integration_states_values')
->setLabel(t('Unpublish state'))
->setDisplayOptions('view', [
'label' => 'hidden',
'region' => 'hidden',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'scheduler_moderation',
'weight' => 30,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', FALSE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->addConstraint('SchedulerUnPublishState')
->addConstraint('SchedulerModerationTransitionAccess');
}
return $fields;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function scheduler_content_moderation_integration_form_node_form_alter(&$form, FormStateInterface $form_state) {
// Attach the publish/un-publish state form elements to the scheduler
// settings group.
$form['publish_state']['#group'] = 'scheduler_settings';
$form['unpublish_state']['#group'] = 'scheduler_settings';
$config = \Drupal::config('scheduler.settings');
/** @var \Drupal\node\NodeTypeInterface $type */
$type = $form_state->getFormObject()->getEntity()->type->entity;
// If scheduling for publish/unpublish is not enabled, then hide the state
// selection field.
$form['publish_state']['#access'] = $type->getThirdPartySetting('scheduler', 'publish_enable', $config->get('default_publish_enable'));
$form['unpublish_state']['#access'] = $type->getThirdPartySetting('scheduler', 'unpublish_enable', $config->get('default_unpublish_enable'));
}
/**
* Helper function for the scheduler moderation widget.
*
* Helps on generating the options dynamically for the scheduler
* moderation widget.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* The field storage definition.
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
* (optional) The entity context if known, or NULL if the allowed values are
* being collected without the context of a specific entity.
* @param bool &$cacheable
* (optional) If an $entity is provided, the $cacheable parameter should be
* modified by reference and set to FALSE if the set of allowed values
* returned was specifically adjusted for that entity and cannot not be reused
* for other entities. Defaults to TRUE.
*
* @return array
* The array of allowed values.
*/
function _scheduler_content_moderation_integration_states_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = FALSE) {
$options = [];
// Fetch all possible states if no entity is given.
if (!$entity) {
$workflow_storage = \Drupal::entityTypeManager()->getStorage('workflow');
foreach ($workflow_storage->loadByProperties(['type' => 'content_moderation']) as $workflow) {
/** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $workflow_type */
$workflow_type = $workflow->getTypePlugin();
foreach ($workflow_type->getStates() as $state_id => $state) {
$options[$workflow->id() . '_' . $state_id] = $state->label();
}
}
return $options;
}
// @todo should call $widget->getEmptyLabel().
$options['_none'] = '';
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_information */
$moderation_information = \Drupal::service('content_moderation.moderation_information');
// Only add options for moderated entities.
if (!$moderation_information->isModeratedEntity($entity)) {
return $options;
}
$workflow = $moderation_information->getWorkflowForEntity($entity);
/** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $type_plugin */
$type_plugin = $workflow->getTypePlugin();
$user = \Drupal::currentUser();
$user_transitions = array_filter($type_plugin->getTransitions(), function (Transition $transition) use ($workflow, $user) {
return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id());
});
foreach ($user_transitions as $transition) {
/** @var \Drupal\content_moderation\ContentModerationState $state */
$state = $transition->to();
if ($state->isDefaultRevisionState() && (($definition->getName() === 'publish_state' && $state->isPublishedState()) || ($definition->getName() === 'unpublish_state' && !$state->isPublishedState()))) {
$options[$state->id()] = $state->label();
}
}
return $options;
}
/**
* Implements hook_scheduler_hide_publish_on_field().
*
* This hook is called from scheduler_form_node_form_alter() and returns TRUE if
* the scheduler publish_on field should be hidden.
*/
function scheduler_content_moderation_integration_scheduler_hide_publish_on_field($form, $form_state, $node) {
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_information */
$moderation_information = \Drupal::service('content_moderation.moderation_information');
$return = FALSE;
if ($moderation_information->isModeratedEntity($node)) {
// If no moderation transitions are available for publish_state then hide
// the publish_on scheduler field.
$options_without_none = array_diff_key($form['publish_state']['widget'][0]['#options'], ['_none' => '']);
$return = (count($options_without_none) == 0);
}
return $return;
}
/**
* Implements hook_scheduler_hide_unpublish_on_field().
*
* This hook is called from scheduler_form_node_form_alter() and returns TRUE if
* the scheduler unpublish_on field should be hidden.
*/
function scheduler_content_moderation_integration_scheduler_hide_unpublish_on_field($form, $form_state, $node) {
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_information */
$moderation_information = \Drupal::service('content_moderation.moderation_information');
$return = FALSE;
if ($moderation_information->isModeratedEntity($node)) {
// If no moderation transitions are available for unpublish_state then hide
// the unpublish_on scheduler field.
$options_without_none = array_diff_key($form['unpublish_state']['widget'][0]['#options'], ['_none' => '']);
$return = (count($options_without_none) == 0);
}
return $return;
}
/**
* Implements hook_scheduler_publish_action().
*
* This hook is called from schedulerManger::publish(). The return values are:
* 1 if the node has been processed here and hence should not be published via
* Scheduler.
* -1 if an exception is thrown, to abandon processing this node in Scheduler.
* 0 if not moderated, to let Scheduler process the node as normal.
*/
function scheduler_content_moderation_integration_scheduler_publish_action($node) {
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_information */
$moderation_information = \Drupal::service('content_moderation.moderation_information');
if (!$moderation_information->isModeratedEntity($node)) {
return 0;
}
$state = $node->publish_state->value;
$node->publish_state->value = NULL;
/** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $type_plugin */
$type_plugin = $moderation_information->getWorkflowForEntity($node)->getTypePlugin();
try {
// If transition is not valid, throw exception.
$type_plugin->getTransitionFromStateToState($node->moderation_state->value, $state);
$node->set('moderation_state', $state);
return 1;
}
catch (\InvalidArgumentException $exception) {
$node->save();
return -1;
}
}
/**
* Implements hook_scheduler_unpublish_action().
*
* This hook is called from schedulerManger::unpublish(). The return values are:
* 1 if the node has been processed here and hence should not be unpublished
* via Scheduler.
* -1 if an exception is thrown, to abandon processing this node in Scheduler.
* 0 if not moderated, to let Scheduler process the node as normal.
*/
function scheduler_content_moderation_integration_scheduler_unpublish_action($node) {
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_information */
$moderation_information = \Drupal::service('content_moderation.moderation_information');
if (!$moderation_information->isModeratedEntity($node)) {
return 0;
}
$state = $node->unpublish_state->value;
$node->unpublish_state->value = NULL;
/** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $type_plugin */
$type_plugin = $moderation_information->getWorkflowForEntity($node)->getTypePlugin();
try {
// If transition is not valid, throw exception.
$type_plugin->getTransitionFromStateToState($node->moderation_state->value, $state);
$node->set('moderation_state', $state);
return 1;
}
catch (\InvalidArgumentException $exception) {
$node->save();
return -1;
}
}