diff 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
line wrap: on
line diff
--- a/core/modules/content_moderation/content_moderation.module	Thu Feb 28 13:11:55 2019 +0000
+++ b/core/modules/content_moderation/content_moderation.module	Thu May 09 15:34:47 2019 +0100
@@ -11,6 +11,7 @@
 use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublish;
 use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublish;
 use Drupal\Core\Access\AccessResult;
+use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityPublishedInterface;
@@ -21,6 +22,9 @@
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Url;
+use Drupal\views\Plugin\views\filter\Broken;
+use Drupal\views\ViewExecutable;
+use Drupal\views\Views;
 use Drupal\workflows\WorkflowInterface;
 use Drupal\Core\Action\Plugin\Action\PublishAction;
 use Drupal\Core\Action\Plugin\Action\UnpublishAction;
@@ -65,6 +69,22 @@
 }
 
 /**
+ * Implements hook_entity_bundle_field_info().
+ */
+function content_moderation_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
+  if (isset($base_field_definitions['moderation_state'])) {
+    // Add the target bundle to the moderation state field. Since each bundle
+    // can be attached to a different moderation workflow, adding this
+    // information to the field definition allows the associated workflow to be
+    // derived where a field definition is present.
+    $base_field_definitions['moderation_state']->setTargetBundle($bundle);
+    return [
+      'moderation_state' => $base_field_definitions['moderation_state'],
+    ];
+  }
+}
+
+/**
  * Implements hook_entity_type_alter().
  */
 function content_moderation_entity_type_alter(array &$entity_types) {
@@ -173,6 +193,19 @@
 }
 
 /**
+ * Implements hook_entity_form_display_alter().
+ */
+function content_moderation_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
+  if ($context['form_mode'] === 'layout_builder') {
+    $form_display->setComponent('moderation_state', [
+      'type' => 'moderation_state_default',
+      'weight' => -900,
+      'settings' => [],
+    ]);
+  }
+}
+
+/**
  * Implements hook_entity_access().
  *
  * Entities should be viewable if unpublished and the user has the appropriate
@@ -207,6 +240,15 @@
     }
   }
 
+  // Do not allow users to delete the state that is configured as the default
+  // state for the workflow.
+  if ($entity instanceof WorkflowInterface) {
+    $configuration = $entity->getTypePlugin()->getConfiguration();
+    if (!empty($configuration['default_moderation_state']) && $operation === sprintf('delete-state:%s', $configuration['default_moderation_state'])) {
+      return AccessResult::forbidden()->addCacheableDependency($entity);
+    }
+  }
+
   return $access_result;
 }
 
@@ -307,6 +349,10 @@
   \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
   // Clear field cache so extra field is added or removed.
   \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
+  // Clear the views data cache so the extra field is available in views.
+  if (\Drupal::moduleHandler()->moduleExists('views')) {
+    Views::viewsData()->clear();
+  }
 }
 
 /**
@@ -318,4 +364,22 @@
   \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
   // Clear field cache so extra field is added or removed.
   \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
+  // Clear the views data cache so the extra field is available in views.
+  if (\Drupal::moduleHandler()->moduleExists('views')) {
+    Views::viewsData()->clear();
+  }
 }
+
+/**
+ * Implements hook_views_post_execute().
+ */
+function content_moderation_views_post_execute(ViewExecutable $view) {
+  // @todo, remove this once broken handlers in views configuration result in
+  // a view no longer returning results. https://www.drupal.org/node/2907954.
+  foreach ($view->filter as $id => $filter) {
+    if (strpos($id, 'moderation_state') === 0 && $filter instanceof Broken) {
+      $view->result = [];
+      break;
+    }
+  }
+}