Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / src / ModerationInformation.php
index c20be5291f2d1bdc41d2e683244337c2b12c3fb8..7e3e513307fe0772d2b0b18a0f109a7445345e0e 100644 (file)
@@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\TypedData\TranslatableInterface;
 
 /**
  * General service for moderation-related questions about Entity API.
@@ -83,14 +84,15 @@ class ModerationInformation implements ModerationInformationInterface {
    */
   public function getLatestRevisionId($entity_type_id, $entity_id) {
     if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
-      $revision_ids = $storage->getQuery()
-        ->allRevisions()
+      $result = $storage->getQuery()
+        ->latestRevision()
         ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
-        ->sort($this->entityTypeManager->getDefinition($entity_type_id)->getKey('revision'), 'DESC')
-        ->range(0, 1)
+        // No access check is performed here since this is an API function and
+        // should return the same ID regardless of the current user.
+        ->accessCheck(FALSE)
         ->execute();
-      if ($revision_ids) {
-        return array_keys($revision_ids)[0];
+      if ($result) {
+        return key($result);
       }
     }
   }
@@ -100,17 +102,38 @@ class ModerationInformation implements ModerationInformationInterface {
    */
   public function getDefaultRevisionId($entity_type_id, $entity_id) {
     if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
-      $revision_ids = $storage->getQuery()
+      $result = $storage->getQuery()
+        ->currentRevision()
         ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
-        ->sort($this->entityTypeManager->getDefinition($entity_type_id)->getKey('revision'), 'DESC')
-        ->range(0, 1)
+        // No access check is performed here since this is an API function and
+        // should return the same ID regardless of the current user.
+        ->accessCheck(FALSE)
         ->execute();
-      if ($revision_ids) {
-        return array_keys($revision_ids)[0];
+      if ($result) {
+        return key($result);
       }
     }
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
+    foreach ($entity->getTranslationLanguages() as $language) {
+      $translation = $entity->getTranslation($language->getId());
+      if (!$translation->isDefaultRevision() && $translation->isRevisionTranslationAffected()) {
+        return $translation;
+      }
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function isPendingRevisionAllowed(ContentEntityInterface $entity) {
+    return !(!$entity->isRevisionTranslationAffected() && count($entity->getTranslationLanguages()) > 1 && $this->hasPendingRevision($entity));
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -121,7 +144,7 @@ class ModerationInformation implements ModerationInformationInterface {
   /**
    * {@inheritdoc}
    */
-  public function hasForwardRevision(ContentEntityInterface $entity) {
+  public function hasPendingRevision(ContentEntityInterface $entity) {
     return $this->isModeratedEntity($entity)
       && !($this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()) == $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id()));
   }
@@ -134,7 +157,30 @@ class ModerationInformation implements ModerationInformationInterface {
     return $this->isLatestRevision($entity)
       && $entity->isDefaultRevision()
       && $entity->moderation_state->value
-      && $workflow->getState($entity->moderation_state->value)->isPublishedState();
+      && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
+    $workflow = $this->getWorkflowForEntity($entity);
+    $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
+
+    // Ensure we are checking all translations of the default revision.
+    if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
+      // Loop through each language that has a translation.
+      foreach ($default_revision->getTranslationLanguages() as $language) {
+        // Load the translated revision.
+        $language_revision = $default_revision->getTranslation($language->getId());
+        // Return TRUE if a translation with a published state is found.
+        if ($workflow->getTypePlugin()->getState($language_revision->moderation_state->value)->isPublishedState()) {
+          return TRUE;
+        }
+      }
+    }
+
+    return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
   }
 
   /**