Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / src / Entity / ContentModerationState.php
index 5b1c250679c05d5bf200c8326c84e3e90d1ee0c5..e54fbbe2a8203e5d835815eea94997dfcaa987bc 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace Drupal\content_moderation\Entity;
 
-use Drupal\content_moderation\ContentModerationStateInterface;
 use Drupal\Core\Entity\ContentEntityBase;
+use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\TypedData\TranslatableInterface;
@@ -39,6 +39,11 @@ use Drupal\user\UserInterface;
  *     "langcode" = "langcode",
  *   }
  * )
+ *
+ * @internal
+ *   This entity is marked internal because it should not be used directly to
+ *   alter the moderation state of an entity. Instead, the computed
+ *   moderation_state field should be set on the entity directly.
  */
 class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
 
@@ -61,7 +66,6 @@ class ContentModerationState extends ContentEntityBase implements ContentModerat
       ->setDescription(t('The workflow the moderation state is in.'))
       ->setSetting('target_type', 'workflow')
       ->setRequired(TRUE)
-      ->setTranslatable(TRUE)
       ->setRevisionable(TRUE);
 
     $fields['moderation_state'] = BaseFieldDefinition::create('string')
@@ -137,6 +141,44 @@ class ContentModerationState extends ContentEntityBase implements ContentModerat
     $content_moderation_state->realSave();
   }
 
+  /**
+   * Loads a content moderation state entity.
+   *
+   * @param \Drupal\Core\Entity\EntityInterface $entity
+   *   A moderated entity object.
+   *
+   * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
+   *   The related content moderation state or NULL if none could be found.
+   *
+   * @internal
+   *   This method should only be called by code directly handling the
+   *   ContentModerationState entity objects.
+   */
+  public static function loadFromModeratedEntity(EntityInterface $entity) {
+    $content_moderation_state = NULL;
+    $moderation_info = \Drupal::service('content_moderation.moderation_information');
+
+    if ($moderation_info->isModeratedEntity($entity)) {
+      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
+      $storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
+
+      $ids = $storage->getQuery()
+        ->condition('content_entity_type_id', $entity->getEntityTypeId())
+        ->condition('content_entity_id', $entity->id())
+        ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id())
+        ->condition('content_entity_revision_id', $entity->getLoadedRevisionId())
+        ->allRevisions()
+        ->execute();
+
+      if ($ids) {
+        /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
+        $content_moderation_state = $storage->loadRevision(key($ids));
+      }
+    }
+
+    return $content_moderation_state;
+  }
+
   /**
    * Default value callback for the 'uid' base field definition.
    *