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 isPendingRevisionAllowed(ContentEntityInterface $entity) {
134 return !(!$entity->isRevisionTranslationAffected() && count($entity->getTranslationLanguages()) > 1 && $this->hasPendingRevision($entity));
140 public function isLatestRevision(ContentEntityInterface $entity) {
141 return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
147 public function hasPendingRevision(ContentEntityInterface $entity) {
148 return $this->isModeratedEntity($entity)
149 && !($this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()) == $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id()));
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();
166 public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
167 $workflow = $this->getWorkflowForEntity($entity);
168 $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
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()) {
183 return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
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']);