Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/content_moderation/src/ModerationInformation.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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 isLatestRevision(ContentEntityInterface $entity) { | |
134 return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()); | |
135 } | |
136 | |
137 /** | |
138 * {@inheritdoc} | |
139 */ | |
140 public function hasPendingRevision(ContentEntityInterface $entity) { | |
141 $result = FALSE; | |
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(); | |
152 } | |
153 } | |
154 return $result; | |
155 } | |
156 | |
157 /** | |
158 * {@inheritdoc} | |
159 */ | |
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(); | |
166 } | |
167 | |
168 /** | |
169 * {@inheritdoc} | |
170 */ | |
171 public function isDefaultRevisionPublished(ContentEntityInterface $entity) { | |
172 $workflow = $this->getWorkflowForEntity($entity); | |
173 $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id()); | |
174 | |
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) { | |
185 continue; | |
186 } | |
187 // Return TRUE if a translation with a published state is found. | |
188 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) { | |
189 return TRUE; | |
190 } | |
191 } | |
192 } | |
193 | |
194 return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState(); | |
195 } | |
196 | |
197 /** | |
198 * {@inheritdoc} | |
199 */ | |
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']); | |
204 }; | |
205 return NULL; | |
206 } | |
207 | |
208 } |