comparison core/modules/content_moderation/content_moderation.module @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
9 use Drupal\content_moderation\EntityTypeInfo; 9 use Drupal\content_moderation\EntityTypeInfo;
10 use Drupal\content_moderation\ContentPreprocess; 10 use Drupal\content_moderation\ContentPreprocess;
11 use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublish; 11 use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublish;
12 use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublish; 12 use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublish;
13 use Drupal\Core\Access\AccessResult; 13 use Drupal\Core\Access\AccessResult;
14 use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
14 use Drupal\Core\Entity\Display\EntityViewDisplayInterface; 15 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
15 use Drupal\Core\Entity\EntityInterface; 16 use Drupal\Core\Entity\EntityInterface;
16 use Drupal\Core\Entity\EntityPublishedInterface; 17 use Drupal\Core\Entity\EntityPublishedInterface;
17 use Drupal\Core\Entity\EntityTypeInterface; 18 use Drupal\Core\Entity\EntityTypeInterface;
18 use Drupal\Core\Field\FieldDefinitionInterface; 19 use Drupal\Core\Field\FieldDefinitionInterface;
19 use Drupal\Core\Field\FieldItemListInterface; 20 use Drupal\Core\Field\FieldItemListInterface;
20 use Drupal\Core\Form\FormStateInterface; 21 use Drupal\Core\Form\FormStateInterface;
21 use Drupal\Core\Routing\RouteMatchInterface; 22 use Drupal\Core\Routing\RouteMatchInterface;
22 use Drupal\Core\Session\AccountInterface; 23 use Drupal\Core\Session\AccountInterface;
23 use Drupal\Core\Url; 24 use Drupal\Core\Url;
25 use Drupal\views\Plugin\views\filter\Broken;
26 use Drupal\views\ViewExecutable;
27 use Drupal\views\Views;
24 use Drupal\workflows\WorkflowInterface; 28 use Drupal\workflows\WorkflowInterface;
25 use Drupal\Core\Action\Plugin\Action\PublishAction; 29 use Drupal\Core\Action\Plugin\Action\PublishAction;
26 use Drupal\Core\Action\Plugin\Action\UnpublishAction; 30 use Drupal\Core\Action\Plugin\Action\UnpublishAction;
27 use Drupal\workflows\Entity\Workflow; 31 use Drupal\workflows\Entity\Workflow;
28 use Drupal\views\Entity\View; 32 use Drupal\views\Entity\View;
63 ->getInstanceFromDefinition(EntityTypeInfo::class) 67 ->getInstanceFromDefinition(EntityTypeInfo::class)
64 ->entityBaseFieldInfo($entity_type); 68 ->entityBaseFieldInfo($entity_type);
65 } 69 }
66 70
67 /** 71 /**
72 * Implements hook_entity_bundle_field_info().
73 */
74 function content_moderation_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
75 if (isset($base_field_definitions['moderation_state'])) {
76 // Add the target bundle to the moderation state field. Since each bundle
77 // can be attached to a different moderation workflow, adding this
78 // information to the field definition allows the associated workflow to be
79 // derived where a field definition is present.
80 $base_field_definitions['moderation_state']->setTargetBundle($bundle);
81 return [
82 'moderation_state' => $base_field_definitions['moderation_state'],
83 ];
84 }
85 }
86
87 /**
68 * Implements hook_entity_type_alter(). 88 * Implements hook_entity_type_alter().
69 */ 89 */
70 function content_moderation_entity_type_alter(array &$entity_types) { 90 function content_moderation_entity_type_alter(array &$entity_types) {
71 \Drupal::service('class_resolver') 91 \Drupal::service('class_resolver')
72 ->getInstanceFromDefinition(EntityTypeInfo::class) 92 ->getInstanceFromDefinition(EntityTypeInfo::class)
168 */ 188 */
169 function content_moderation_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { 189 function content_moderation_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
170 \Drupal::service('class_resolver') 190 \Drupal::service('class_resolver')
171 ->getInstanceFromDefinition(EntityOperations::class) 191 ->getInstanceFromDefinition(EntityOperations::class)
172 ->entityView($build, $entity, $display, $view_mode); 192 ->entityView($build, $entity, $display, $view_mode);
193 }
194
195 /**
196 * Implements hook_entity_form_display_alter().
197 */
198 function content_moderation_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
199 if ($context['form_mode'] === 'layout_builder') {
200 $form_display->setComponent('moderation_state', [
201 'type' => 'moderation_state_default',
202 'weight' => -900,
203 'settings' => [],
204 ]);
205 }
173 } 206 }
174 207
175 /** 208 /**
176 * Implements hook_entity_access(). 209 * Implements hook_entity_access().
177 * 210 *
202 $access_result->addCacheableDependency($account); 235 $access_result->addCacheableDependency($account);
203 $workflow = $moderation_info->getWorkflowForEntity($entity); 236 $workflow = $moderation_info->getWorkflowForEntity($entity);
204 $access_result->addCacheableDependency($workflow); 237 $access_result->addCacheableDependency($workflow);
205 foreach ($valid_transition_targets as $valid_transition_target) { 238 foreach ($valid_transition_targets as $valid_transition_target) {
206 $access_result->addCacheableDependency($valid_transition_target); 239 $access_result->addCacheableDependency($valid_transition_target);
240 }
241 }
242
243 // Do not allow users to delete the state that is configured as the default
244 // state for the workflow.
245 if ($entity instanceof WorkflowInterface) {
246 $configuration = $entity->getTypePlugin()->getConfiguration();
247 if (!empty($configuration['default_moderation_state']) && $operation === sprintf('delete-state:%s', $configuration['default_moderation_state'])) {
248 return AccessResult::forbidden()->addCacheableDependency($entity);
207 } 249 }
208 } 250 }
209 251
210 return $access_result; 252 return $access_result;
211 } 253 }
305 // Clear bundle cache so workflow gets added or removed from the bundle 347 // Clear bundle cache so workflow gets added or removed from the bundle
306 // information. 348 // information.
307 \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); 349 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
308 // Clear field cache so extra field is added or removed. 350 // Clear field cache so extra field is added or removed.
309 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); 351 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
352 // Clear the views data cache so the extra field is available in views.
353 if (\Drupal::moduleHandler()->moduleExists('views')) {
354 Views::viewsData()->clear();
355 }
310 } 356 }
311 357
312 /** 358 /**
313 * Implements hook_ENTITY_TYPE_update(). 359 * Implements hook_ENTITY_TYPE_update().
314 */ 360 */
316 // Clear bundle cache so workflow gets added or removed from the bundle 362 // Clear bundle cache so workflow gets added or removed from the bundle
317 // information. 363 // information.
318 \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); 364 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
319 // Clear field cache so extra field is added or removed. 365 // Clear field cache so extra field is added or removed.
320 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); 366 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
321 } 367 // Clear the views data cache so the extra field is available in views.
368 if (\Drupal::moduleHandler()->moduleExists('views')) {
369 Views::viewsData()->clear();
370 }
371 }
372
373 /**
374 * Implements hook_views_post_execute().
375 */
376 function content_moderation_views_post_execute(ViewExecutable $view) {
377 // @todo, remove this once broken handlers in views configuration result in
378 // a view no longer returning results. https://www.drupal.org/node/2907954.
379 foreach ($view->filter as $id => $filter) {
380 if (strpos($id, 'moderation_state') === 0 && $filter instanceof Broken) {
381 $view->result = [];
382 break;
383 }
384 }
385 }