Version 1
[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
11 /**
12  * General service for moderation-related questions about Entity API.
13  */
14 class ModerationInformation implements ModerationInformationInterface {
15
16   /**
17    * The entity type manager.
18    *
19    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
20    */
21   protected $entityTypeManager;
22
23   /**
24    * The bundle information service.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
27    */
28   protected $bundleInfo;
29
30   /**
31    * Creates a new ModerationInformation instance.
32    *
33    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
34    *   The entity type manager.
35    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
36    *   The bundle information service.
37    */
38   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
39     $this->entityTypeManager = $entity_type_manager;
40     $this->bundleInfo = $bundle_info;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function isModeratedEntity(EntityInterface $entity) {
47     if (!$entity instanceof ContentEntityInterface) {
48       return FALSE;
49     }
50
51     return $this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle());
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
58     return $entity_type->hasHandlerClass('moderation');
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
65     if ($this->canModerateEntitiesOfEntityType($entity_type)) {
66       $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
67       return isset($bundles[$bundle]['workflow']);
68     }
69     return FALSE;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function getLatestRevision($entity_type_id, $entity_id) {
76     if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) {
77       return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id);
78     }
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getLatestRevisionId($entity_type_id, $entity_id) {
85     if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
86       $revision_ids = $storage->getQuery()
87         ->allRevisions()
88         ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
89         ->sort($this->entityTypeManager->getDefinition($entity_type_id)->getKey('revision'), 'DESC')
90         ->range(0, 1)
91         ->execute();
92       if ($revision_ids) {
93         return array_keys($revision_ids)[0];
94       }
95     }
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getDefaultRevisionId($entity_type_id, $entity_id) {
102     if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
103       $revision_ids = $storage->getQuery()
104         ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
105         ->sort($this->entityTypeManager->getDefinition($entity_type_id)->getKey('revision'), 'DESC')
106         ->range(0, 1)
107         ->execute();
108       if ($revision_ids) {
109         return array_keys($revision_ids)[0];
110       }
111     }
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function isLatestRevision(ContentEntityInterface $entity) {
118     return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function hasForwardRevision(ContentEntityInterface $entity) {
125     return $this->isModeratedEntity($entity)
126       && !($this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()) == $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id()));
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function isLiveRevision(ContentEntityInterface $entity) {
133     $workflow = $this->getWorkflowForEntity($entity);
134     return $this->isLatestRevision($entity)
135       && $entity->isDefaultRevision()
136       && $entity->moderation_state->value
137       && $workflow->getState($entity->moderation_state->value)->isPublishedState();
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   public function getWorkflowForEntity(ContentEntityInterface $entity) {
144     $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
145     if (isset($bundles[$entity->bundle()]['workflow'])) {
146       return $this->entityTypeManager->getStorage('workflow')->load($bundles[$entity->bundle()]['workflow']);
147     };
148     return NULL;
149   }
150
151 }