3 namespace Drupal\content_moderation;
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;
13 * General service for moderation-related questions about Entity API.
15 class ModerationInformation implements ModerationInformationInterface {
18 * The entity type manager.
20 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22 protected $entityTypeManager;
25 * The bundle information service.
27 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
29 protected $bundleInfo;
32 * Creates a new ModerationInformation instance.
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.
39 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
40 $this->entityTypeManager = $entity_type_manager;
41 $this->bundleInfo = $bundle_info;
47 public function isModeratedEntity(EntityInterface $entity) {
48 if (!$entity instanceof ContentEntityInterface) {
52 return $this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle());
58 public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
59 return $entity_type->hasHandlerClass('moderation');
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']);
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);
85 public function getLatestRevisionId($entity_type_id, $entity_id) {
86 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
87 $result = $storage->getQuery()
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.
103 public function getDefaultRevisionId($entity_type_id, $entity_id) {
104 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
105 $result = $storage->getQuery()
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.
121 public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
122 foreach ($entity->getTranslationLanguages() as $language) {
123 $translation = $entity->getTranslation($language->getId());
124 if (!$translation->isDefaultRevision() && $translation->isRevisionTranslationAffected()) {
133 public function isLatestRevision(ContentEntityInterface $entity) {
134 return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
140 public function hasPendingRevision(ContentEntityInterface $entity) {
142 if ($this->isModeratedEntity($entity)) {
143 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
144 $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
145 $latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId());
146 $default_revision_id = $entity->isDefaultRevision() && !$entity->isNewRevision() && ($revision_id = $entity->getRevisionId()) ?
147 $revision_id : $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id());
148 if ($latest_revision_id != $default_revision_id) {
149 /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
150 $latest_revision = $storage->loadRevision($latest_revision_id);
151 $result = !$latest_revision->wasDefaultRevision();
160 public function isLiveRevision(ContentEntityInterface $entity) {
161 $workflow = $this->getWorkflowForEntity($entity);
162 return $this->isLatestRevision($entity)
163 && $entity->isDefaultRevision()
164 && $entity->moderation_state->value
165 && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
171 public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
172 $workflow = $this->getWorkflowForEntity($entity);
173 $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
175 // Ensure we are checking all translations of the default revision.
176 if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
177 // Loop through each language that has a translation.
178 foreach ($default_revision->getTranslationLanguages() as $language) {
179 // Load the translated revision.
180 $translation = $default_revision->getTranslation($language->getId());
181 // If the moderation state is empty, it was not stored yet so no point
182 // in doing further work.
183 $moderation_state = $translation->moderation_state->value;
184 if (!$moderation_state) {
187 // Return TRUE if a translation with a published state is found.
188 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) {
194 return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
200 public function getWorkflowForEntity(ContentEntityInterface $entity) {
201 $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
202 if (isset($bundles[$entity->bundle()]['workflow'])) {
203 return $this->entityTypeManager->getStorage('workflow')->load($bundles[$entity->bundle()]['workflow']);