Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / src / ModerationInformation.php
1 <?php
2
3 namespace Drupal\content_moderation;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\TypedData\TranslatableInterface;
11
12 /**
13  * General service for moderation-related questions about Entity API.
14  */
15 class ModerationInformation implements ModerationInformationInterface {
16
17   /**
18    * The entity type manager.
19    *
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * The bundle information service.
26    *
27    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
28    */
29   protected $bundleInfo;
30
31   /**
32    * Creates a new ModerationInformation instance.
33    *
34    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
35    *   The entity type manager.
36    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
37    *   The bundle information service.
38    */
39   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
40     $this->entityTypeManager = $entity_type_manager;
41     $this->bundleInfo = $bundle_info;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function isModeratedEntity(EntityInterface $entity) {
48     if (!$entity instanceof ContentEntityInterface) {
49       return FALSE;
50     }
51
52     return $this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle());
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
59     return $entity_type->hasHandlerClass('moderation');
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
66     if ($this->canModerateEntitiesOfEntityType($entity_type)) {
67       $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
68       return isset($bundles[$bundle]['workflow']);
69     }
70     return FALSE;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getLatestRevision($entity_type_id, $entity_id) {
77     if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) {
78       return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id);
79     }
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getLatestRevisionId($entity_type_id, $entity_id) {
86     if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
87       $result = $storage->getQuery()
88         ->latestRevision()
89         ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
90         // No access check is performed here since this is an API function and
91         // should return the same ID regardless of the current user.
92         ->accessCheck(FALSE)
93         ->execute();
94       if ($result) {
95         return key($result);
96       }
97     }
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getDefaultRevisionId($entity_type_id, $entity_id) {
104     if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
105       $result = $storage->getQuery()
106         ->currentRevision()
107         ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
108         // No access check is performed here since this is an API function and
109         // should return the same ID regardless of the current user.
110         ->accessCheck(FALSE)
111         ->execute();
112       if ($result) {
113         return key($result);
114       }
115     }
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
122     foreach ($entity->getTranslationLanguages() as $language) {
123       $translation = $entity->getTranslation($language->getId());
124       if (!$translation->isDefaultRevision() && $translation->isRevisionTranslationAffected()) {
125         return $translation;
126       }
127     }
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function isPendingRevisionAllowed(ContentEntityInterface $entity) {
134     return !(!$entity->isRevisionTranslationAffected() && count($entity->getTranslationLanguages()) > 1 && $this->hasPendingRevision($entity));
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function isLatestRevision(ContentEntityInterface $entity) {
141     return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function hasPendingRevision(ContentEntityInterface $entity) {
148     return $this->isModeratedEntity($entity)
149       && !($this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()) == $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id()));
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function isLiveRevision(ContentEntityInterface $entity) {
156     $workflow = $this->getWorkflowForEntity($entity);
157     return $this->isLatestRevision($entity)
158       && $entity->isDefaultRevision()
159       && $entity->moderation_state->value
160       && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
167     $workflow = $this->getWorkflowForEntity($entity);
168     $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
169
170     // Ensure we are checking all translations of the default revision.
171     if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
172       // Loop through each language that has a translation.
173       foreach ($default_revision->getTranslationLanguages() as $language) {
174         // Load the translated revision.
175         $language_revision = $default_revision->getTranslation($language->getId());
176         // Return TRUE if a translation with a published state is found.
177         if ($workflow->getTypePlugin()->getState($language_revision->moderation_state->value)->isPublishedState()) {
178           return TRUE;
179         }
180       }
181     }
182
183     return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
184   }
185
186   /**
187    * {@inheritdoc}
188    */
189   public function getWorkflowForEntity(ContentEntityInterface $entity) {
190     $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
191     if (isset($bundles[$entity->bundle()]['workflow'])) {
192       return $this->entityTypeManager->getStorage('workflow')->load($bundles[$entity->bundle()]['workflow']);
193     };
194     return NULL;
195   }
196
197 }