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