X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcontent_moderation%2Fsrc%2FEntityOperations.php;fp=web%2Fcore%2Fmodules%2Fcontent_moderation%2Fsrc%2FEntityOperations.php;h=9ab40eac05691fb70202c4bbfb4f26377690d5a2;hp=e2688601df5a77c9778dc95d78ef29828a63ec20;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/content_moderation/src/EntityOperations.php b/web/core/modules/content_moderation/src/EntityOperations.php index e2688601d..9ab40eac0 100644 --- a/web/core/modules/content_moderation/src/EntityOperations.php +++ b/web/core/modules/content_moderation/src/EntityOperations.php @@ -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; + } + }