annotate core/modules/content_moderation/src/ModerationInformation.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
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@17 7 use Drupal\Core\Entity\EntityPublishedInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
Chris@0 9 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 10 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 11 use Drupal\Core\TypedData\TranslatableInterface;
Chris@17 12 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * General service for moderation-related questions about Entity API.
Chris@0 16 */
Chris@0 17 class ModerationInformation implements ModerationInformationInterface {
Chris@0 18
Chris@17 19 use StringTranslationTrait;
Chris@17 20
Chris@0 21 /**
Chris@0 22 * The entity type manager.
Chris@0 23 *
Chris@0 24 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 25 */
Chris@0 26 protected $entityTypeManager;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The bundle information service.
Chris@0 30 *
Chris@0 31 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
Chris@0 32 */
Chris@0 33 protected $bundleInfo;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Creates a new ModerationInformation instance.
Chris@0 37 *
Chris@0 38 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 39 * The entity type manager.
Chris@0 40 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
Chris@0 41 * The bundle information service.
Chris@0 42 */
Chris@0 43 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
Chris@0 44 $this->entityTypeManager = $entity_type_manager;
Chris@0 45 $this->bundleInfo = $bundle_info;
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * {@inheritdoc}
Chris@0 50 */
Chris@0 51 public function isModeratedEntity(EntityInterface $entity) {
Chris@0 52 if (!$entity instanceof ContentEntityInterface) {
Chris@0 53 return FALSE;
Chris@0 54 }
Chris@0 55
Chris@0 56 return $this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle());
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * {@inheritdoc}
Chris@0 61 */
Chris@18 62 public function isModeratedEntityType(EntityTypeInterface $entity_type) {
Chris@18 63 $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
Chris@18 64 return !empty(array_column($bundles, 'workflow'));
Chris@18 65 }
Chris@18 66
Chris@18 67 /**
Chris@18 68 * {@inheritdoc}
Chris@18 69 */
Chris@0 70 public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
Chris@0 71 return $entity_type->hasHandlerClass('moderation');
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
Chris@0 78 if ($this->canModerateEntitiesOfEntityType($entity_type)) {
Chris@0 79 $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
Chris@0 80 return isset($bundles[$bundle]['workflow']);
Chris@0 81 }
Chris@0 82 return FALSE;
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * {@inheritdoc}
Chris@0 87 */
Chris@0 88 public function getLatestRevision($entity_type_id, $entity_id) {
Chris@0 89 if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) {
Chris@0 90 return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id);
Chris@0 91 }
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@0 97 public function getLatestRevisionId($entity_type_id, $entity_id) {
Chris@0 98 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
Chris@12 99 $result = $storage->getQuery()
Chris@12 100 ->latestRevision()
Chris@0 101 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
Chris@12 102 // No access check is performed here since this is an API function and
Chris@12 103 // should return the same ID regardless of the current user.
Chris@12 104 ->accessCheck(FALSE)
Chris@0 105 ->execute();
Chris@12 106 if ($result) {
Chris@12 107 return key($result);
Chris@0 108 }
Chris@0 109 }
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * {@inheritdoc}
Chris@0 114 */
Chris@0 115 public function getDefaultRevisionId($entity_type_id, $entity_id) {
Chris@0 116 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
Chris@12 117 $result = $storage->getQuery()
Chris@12 118 ->currentRevision()
Chris@0 119 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
Chris@12 120 // No access check is performed here since this is an API function and
Chris@12 121 // should return the same ID regardless of the current user.
Chris@12 122 ->accessCheck(FALSE)
Chris@0 123 ->execute();
Chris@12 124 if ($result) {
Chris@12 125 return key($result);
Chris@0 126 }
Chris@0 127 }
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * {@inheritdoc}
Chris@0 132 */
Chris@0 133 public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
Chris@0 134 foreach ($entity->getTranslationLanguages() as $language) {
Chris@0 135 $translation = $entity->getTranslation($language->getId());
Chris@0 136 if (!$translation->isDefaultRevision() && $translation->isRevisionTranslationAffected()) {
Chris@0 137 return $translation;
Chris@0 138 }
Chris@0 139 }
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * {@inheritdoc}
Chris@0 144 */
Chris@0 145 public function isLatestRevision(ContentEntityInterface $entity) {
Chris@0 146 return $entity->getRevisionId() == $this->getLatestRevisionId($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 hasPendingRevision(ContentEntityInterface $entity) {
Chris@14 153 $result = FALSE;
Chris@14 154 if ($this->isModeratedEntity($entity)) {
Chris@14 155 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
Chris@14 156 $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
Chris@14 157 $latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId());
Chris@14 158 $default_revision_id = $entity->isDefaultRevision() && !$entity->isNewRevision() && ($revision_id = $entity->getRevisionId()) ?
Chris@14 159 $revision_id : $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id());
Chris@14 160 if ($latest_revision_id != $default_revision_id) {
Chris@14 161 /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
Chris@14 162 $latest_revision = $storage->loadRevision($latest_revision_id);
Chris@14 163 $result = !$latest_revision->wasDefaultRevision();
Chris@14 164 }
Chris@14 165 }
Chris@14 166 return $result;
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * {@inheritdoc}
Chris@0 171 */
Chris@0 172 public function isLiveRevision(ContentEntityInterface $entity) {
Chris@0 173 $workflow = $this->getWorkflowForEntity($entity);
Chris@0 174 return $this->isLatestRevision($entity)
Chris@0 175 && $entity->isDefaultRevision()
Chris@0 176 && $entity->moderation_state->value
Chris@0 177 && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * {@inheritdoc}
Chris@0 182 */
Chris@0 183 public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
Chris@0 184 $workflow = $this->getWorkflowForEntity($entity);
Chris@0 185 $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
Chris@17 186 // If no default revision could be loaded, the entity has not yet been
Chris@17 187 // saved. In this case the moderation_state of the unsaved entity can be
Chris@17 188 // used, since once saved it will become the default.
Chris@17 189 $default_revision = $default_revision ?: $entity;
Chris@0 190
Chris@0 191 // Ensure we are checking all translations of the default revision.
Chris@0 192 if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
Chris@0 193 // Loop through each language that has a translation.
Chris@0 194 foreach ($default_revision->getTranslationLanguages() as $language) {
Chris@0 195 // Load the translated revision.
Chris@14 196 $translation = $default_revision->getTranslation($language->getId());
Chris@14 197 // If the moderation state is empty, it was not stored yet so no point
Chris@14 198 // in doing further work.
Chris@14 199 $moderation_state = $translation->moderation_state->value;
Chris@14 200 if (!$moderation_state) {
Chris@14 201 continue;
Chris@14 202 }
Chris@0 203 // Return TRUE if a translation with a published state is found.
Chris@14 204 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) {
Chris@0 205 return TRUE;
Chris@0 206 }
Chris@0 207 }
Chris@0 208 }
Chris@0 209
Chris@0 210 return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
Chris@0 211 }
Chris@0 212
Chris@0 213 /**
Chris@0 214 * {@inheritdoc}
Chris@0 215 */
Chris@0 216 public function getWorkflowForEntity(ContentEntityInterface $entity) {
Chris@18 217 return $this->getWorkflowForEntityTypeAndBundle($entity->getEntityTypeId(), $entity->bundle());
Chris@18 218 }
Chris@18 219
Chris@18 220 /**
Chris@18 221 * {@inheritdoc}
Chris@18 222 */
Chris@18 223 public function getWorkflowForEntityTypeAndBundle($entity_type_id, $bundle_id) {
Chris@18 224 $bundles = $this->bundleInfo->getBundleInfo($entity_type_id);
Chris@18 225 if (isset($bundles[$bundle_id]['workflow'])) {
Chris@18 226 return $this->entityTypeManager->getStorage('workflow')->load($bundles[$bundle_id]['workflow']);
Chris@18 227 }
Chris@0 228 return NULL;
Chris@0 229 }
Chris@0 230
Chris@17 231 /**
Chris@17 232 * {@inheritdoc}
Chris@17 233 */
Chris@17 234 public function getUnsupportedFeatures(EntityTypeInterface $entity_type) {
Chris@17 235 $features = [];
Chris@17 236 // Test if entity is publishable.
Chris@17 237 if (!$entity_type->entityClassImplements(EntityPublishedInterface::class)) {
Chris@17 238 $features['publishing'] = $this->t("@entity_type_plural_label do not support publishing statuses. For example, even after transitioning from a published workflow state to an unpublished workflow state they will still be visible to site visitors.", ['@entity_type_plural_label' => $entity_type->getCollectionLabel()]);
Chris@17 239 }
Chris@17 240 return $features;
Chris@17 241 }
Chris@17 242
Chris@0 243 }