Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_moderation / src / EntityOperations.php
index e2688601df5a77c9778dc95d78ef29828a63ec20..9ab40eac05691fb70202c4bbfb4f26377690d5a2 100644 (file)
@@ -5,8 +5,10 @@ namespace Drupal\content_moderation;
 use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity;
 use Drupal\content_moderation\Entity\ContentModerationStateInterface;
 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
+use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\EntityPublishedInterface;
 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormBuilderInterface;
@@ -203,7 +205,6 @@ class EntityOperations implements ContainerInjectionInterface {
       $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id();
     }
 
-    // @todo what if $entity->moderation_state is null at this point?
     $content_moderation_state->set('content_entity_revision_id', $entity_revision_id);
     $content_moderation_state->set('moderation_state', $moderation_state);
     ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
@@ -279,17 +280,39 @@ class EntityOperations implements ContainerInjectionInterface {
     // The moderation form should be displayed only when viewing the latest
     // (translation-affecting) revision, unless it was created as published
     // default revision.
-    if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
+    if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && $this->isPublished($entity)) {
       return;
     }
-    if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && ($moderation_state = $entity->get('moderation_state')->value)) {
-      $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
-      if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) {
-        return;
-      }
+    if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
+      return;
     }
 
     $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
   }
 
+  /**
+   * Checks if the entity is published.
+   *
+   * This method is optimized to not have to unnecessarily load the moderation
+   * state and workflow if it is not required.
+   *
+   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
+   *   The entity to check.
+   *
+   * @return bool
+   *   TRUE if the entity is published, FALSE otherwise.
+   */
+  protected function isPublished(ContentEntityInterface $entity) {
+    // If the entity implements EntityPublishedInterface directly, check that
+    // first, otherwise fall back to check through the workflow state.
+    if ($entity instanceof EntityPublishedInterface) {
+      return $entity->isPublished();
+    }
+    if ($moderation_state = $entity->get('moderation_state')->value) {
+      $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
+      return $workflow->getTypePlugin()->getState($moderation_state)->isPublishedState();
+    }
+    return FALSE;
+  }
+
 }