annotate core/modules/content_moderation/src/ModerationInformation.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
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@4 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@4 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@4 19 use StringTranslationTrait;
Chris@4 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@0 62 public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
Chris@0 63 return $entity_type->hasHandlerClass('moderation');
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * {@inheritdoc}
Chris@0 68 */
Chris@0 69 public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
Chris@0 70 if ($this->canModerateEntitiesOfEntityType($entity_type)) {
Chris@0 71 $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
Chris@0 72 return isset($bundles[$bundle]['workflow']);
Chris@0 73 }
Chris@0 74 return FALSE;
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * {@inheritdoc}
Chris@0 79 */
Chris@0 80 public function getLatestRevision($entity_type_id, $entity_id) {
Chris@0 81 if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) {
Chris@0 82 return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id);
Chris@0 83 }
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * {@inheritdoc}
Chris@0 88 */
Chris@0 89 public function getLatestRevisionId($entity_type_id, $entity_id) {
Chris@0 90 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
Chris@0 91 $result = $storage->getQuery()
Chris@0 92 ->latestRevision()
Chris@0 93 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
Chris@0 94 // No access check is performed here since this is an API function and
Chris@0 95 // should return the same ID regardless of the current user.
Chris@0 96 ->accessCheck(FALSE)
Chris@0 97 ->execute();
Chris@0 98 if ($result) {
Chris@0 99 return key($result);
Chris@0 100 }
Chris@0 101 }
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * {@inheritdoc}
Chris@0 106 */
Chris@0 107 public function getDefaultRevisionId($entity_type_id, $entity_id) {
Chris@0 108 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
Chris@0 109 $result = $storage->getQuery()
Chris@0 110 ->currentRevision()
Chris@0 111 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
Chris@0 112 // No access check is performed here since this is an API function and
Chris@0 113 // should return the same ID regardless of the current user.
Chris@0 114 ->accessCheck(FALSE)
Chris@0 115 ->execute();
Chris@0 116 if ($result) {
Chris@0 117 return key($result);
Chris@0 118 }
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * {@inheritdoc}
Chris@0 124 */
Chris@0 125 public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
Chris@0 126 foreach ($entity->getTranslationLanguages() as $language) {
Chris@0 127 $translation = $entity->getTranslation($language->getId());
Chris@0 128 if (!$translation->isDefaultRevision() && $translation->isRevisionTranslationAffected()) {
Chris@0 129 return $translation;
Chris@0 130 }
Chris@0 131 }
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 $result = FALSE;
Chris@0 146 if ($this->isModeratedEntity($entity)) {
Chris@0 147 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
Chris@0 148 $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
Chris@0 149 $latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId());
Chris@0 150 $default_revision_id = $entity->isDefaultRevision() && !$entity->isNewRevision() && ($revision_id = $entity->getRevisionId()) ?
Chris@0 151 $revision_id : $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id());
Chris@0 152 if ($latest_revision_id != $default_revision_id) {
Chris@0 153 /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
Chris@0 154 $latest_revision = $storage->loadRevision($latest_revision_id);
Chris@0 155 $result = !$latest_revision->wasDefaultRevision();
Chris@0 156 }
Chris@0 157 }
Chris@0 158 return $result;
Chris@0 159 }
Chris@0 160
Chris@0 161 /**
Chris@0 162 * {@inheritdoc}
Chris@0 163 */
Chris@0 164 public function isLiveRevision(ContentEntityInterface $entity) {
Chris@0 165 $workflow = $this->getWorkflowForEntity($entity);
Chris@0 166 return $this->isLatestRevision($entity)
Chris@0 167 && $entity->isDefaultRevision()
Chris@0 168 && $entity->moderation_state->value
Chris@0 169 && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * {@inheritdoc}
Chris@0 174 */
Chris@0 175 public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
Chris@0 176 $workflow = $this->getWorkflowForEntity($entity);
Chris@0 177 $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
Chris@4 178 // If no default revision could be loaded, the entity has not yet been
Chris@4 179 // saved. In this case the moderation_state of the unsaved entity can be
Chris@4 180 // used, since once saved it will become the default.
Chris@4 181 $default_revision = $default_revision ?: $entity;
Chris@0 182
Chris@0 183 // Ensure we are checking all translations of the default revision.
Chris@0 184 if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
Chris@0 185 // Loop through each language that has a translation.
Chris@0 186 foreach ($default_revision->getTranslationLanguages() as $language) {
Chris@0 187 // Load the translated revision.
Chris@0 188 $translation = $default_revision->getTranslation($language->getId());
Chris@0 189 // If the moderation state is empty, it was not stored yet so no point
Chris@0 190 // in doing further work.
Chris@0 191 $moderation_state = $translation->moderation_state->value;
Chris@0 192 if (!$moderation_state) {
Chris@0 193 continue;
Chris@0 194 }
Chris@0 195 // Return TRUE if a translation with a published state is found.
Chris@0 196 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) {
Chris@0 197 return TRUE;
Chris@0 198 }
Chris@0 199 }
Chris@0 200 }
Chris@0 201
Chris@0 202 return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
Chris@0 203 }
Chris@0 204
Chris@0 205 /**
Chris@0 206 * {@inheritdoc}
Chris@0 207 */
Chris@0 208 public function getWorkflowForEntity(ContentEntityInterface $entity) {
Chris@0 209 $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
Chris@0 210 if (isset($bundles[$entity->bundle()]['workflow'])) {
Chris@0 211 return $this->entityTypeManager->getStorage('workflow')->load($bundles[$entity->bundle()]['workflow']);
Chris@0 212 };
Chris@0 213 return NULL;
Chris@0 214 }
Chris@0 215
Chris@4 216 /**
Chris@4 217 * {@inheritdoc}
Chris@4 218 */
Chris@4 219 public function getUnsupportedFeatures(EntityTypeInterface $entity_type) {
Chris@4 220 $features = [];
Chris@4 221 // Test if entity is publishable.
Chris@4 222 if (!$entity_type->entityClassImplements(EntityPublishedInterface::class)) {
Chris@4 223 $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@4 224 }
Chris@4 225 return $features;
Chris@4 226 }
Chris@4 227
Chris@0 228 }