annotate 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
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@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 $result = $storage->getQuery()
Chris@0 88 ->latestRevision()
Chris@0 89 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
Chris@0 90 // No access check is performed here since this is an API function and
Chris@0 91 // should return the same ID regardless of the current user.
Chris@0 92 ->accessCheck(FALSE)
Chris@0 93 ->execute();
Chris@0 94 if ($result) {
Chris@0 95 return key($result);
Chris@0 96 }
Chris@0 97 }
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * {@inheritdoc}
Chris@0 102 */
Chris@0 103 public function getDefaultRevisionId($entity_type_id, $entity_id) {
Chris@0 104 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
Chris@0 105 $result = $storage->getQuery()
Chris@0 106 ->currentRevision()
Chris@0 107 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
Chris@0 108 // No access check is performed here since this is an API function and
Chris@0 109 // should return the same ID regardless of the current user.
Chris@0 110 ->accessCheck(FALSE)
Chris@0 111 ->execute();
Chris@0 112 if ($result) {
Chris@0 113 return key($result);
Chris@0 114 }
Chris@0 115 }
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 */
Chris@0 121 public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
Chris@0 122 foreach ($entity->getTranslationLanguages() as $language) {
Chris@0 123 $translation = $entity->getTranslation($language->getId());
Chris@0 124 if (!$translation->isDefaultRevision() && $translation->isRevisionTranslationAffected()) {
Chris@0 125 return $translation;
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 isLatestRevision(ContentEntityInterface $entity) {
Chris@0 134 return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * {@inheritdoc}
Chris@0 139 */
Chris@0 140 public function hasPendingRevision(ContentEntityInterface $entity) {
Chris@0 141 $result = FALSE;
Chris@0 142 if ($this->isModeratedEntity($entity)) {
Chris@0 143 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
Chris@0 144 $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
Chris@0 145 $latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId());
Chris@0 146 $default_revision_id = $entity->isDefaultRevision() && !$entity->isNewRevision() && ($revision_id = $entity->getRevisionId()) ?
Chris@0 147 $revision_id : $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id());
Chris@0 148 if ($latest_revision_id != $default_revision_id) {
Chris@0 149 /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
Chris@0 150 $latest_revision = $storage->loadRevision($latest_revision_id);
Chris@0 151 $result = !$latest_revision->wasDefaultRevision();
Chris@0 152 }
Chris@0 153 }
Chris@0 154 return $result;
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * {@inheritdoc}
Chris@0 159 */
Chris@0 160 public function isLiveRevision(ContentEntityInterface $entity) {
Chris@0 161 $workflow = $this->getWorkflowForEntity($entity);
Chris@0 162 return $this->isLatestRevision($entity)
Chris@0 163 && $entity->isDefaultRevision()
Chris@0 164 && $entity->moderation_state->value
Chris@0 165 && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * {@inheritdoc}
Chris@0 170 */
Chris@0 171 public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
Chris@0 172 $workflow = $this->getWorkflowForEntity($entity);
Chris@0 173 $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
Chris@0 174
Chris@0 175 // Ensure we are checking all translations of the default revision.
Chris@0 176 if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
Chris@0 177 // Loop through each language that has a translation.
Chris@0 178 foreach ($default_revision->getTranslationLanguages() as $language) {
Chris@0 179 // Load the translated revision.
Chris@0 180 $translation = $default_revision->getTranslation($language->getId());
Chris@0 181 // If the moderation state is empty, it was not stored yet so no point
Chris@0 182 // in doing further work.
Chris@0 183 $moderation_state = $translation->moderation_state->value;
Chris@0 184 if (!$moderation_state) {
Chris@0 185 continue;
Chris@0 186 }
Chris@0 187 // Return TRUE if a translation with a published state is found.
Chris@0 188 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) {
Chris@0 189 return TRUE;
Chris@0 190 }
Chris@0 191 }
Chris@0 192 }
Chris@0 193
Chris@0 194 return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
Chris@0 195 }
Chris@0 196
Chris@0 197 /**
Chris@0 198 * {@inheritdoc}
Chris@0 199 */
Chris@0 200 public function getWorkflowForEntity(ContentEntityInterface $entity) {
Chris@0 201 $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
Chris@0 202 if (isset($bundles[$entity->bundle()]['workflow'])) {
Chris@0 203 return $this->entityTypeManager->getStorage('workflow')->load($bundles[$entity->bundle()]['workflow']);
Chris@0 204 };
Chris@0 205 return NULL;
Chris@0 206 }
Chris@0 207
Chris@0 208 }