ModerationInformation.php in Drupal 10
File
core/modules/content_moderation/src/ModerationInformation.php
View source
<?php
namespace Drupal\content_moderation;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class ModerationInformation implements ModerationInformationInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $bundleInfo;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->bundleInfo = $bundle_info;
}
public function isModeratedEntity(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface) {
return FALSE;
}
if (!$this
->shouldModerateEntitiesOfBundle($entity
->getEntityType(), $entity
->bundle())) {
return FALSE;
}
return $this->entityTypeManager
->getHandler($entity
->getEntityTypeId(), 'moderation')
->isModeratedEntity($entity);
}
public function isModeratedEntityType(EntityTypeInterface $entity_type) {
$bundles = $this->bundleInfo
->getBundleInfo($entity_type
->id());
return !empty(array_column($bundles, 'workflow'));
}
public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
return $entity_type
->hasHandlerClass('moderation');
}
public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
if ($this
->canModerateEntitiesOfEntityType($entity_type)) {
$bundles = $this->bundleInfo
->getBundleInfo($entity_type
->id());
return isset($bundles[$bundle]['workflow']);
}
return FALSE;
}
public function getDefaultRevisionId($entity_type_id, $entity_id) {
if ($storage = $this->entityTypeManager
->getStorage($entity_type_id)) {
$result = $storage
->getQuery()
->currentRevision()
->condition($this->entityTypeManager
->getDefinition($entity_type_id)
->getKey('id'), $entity_id)
->accessCheck(FALSE)
->execute();
if ($result) {
return key($result);
}
}
}
public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
foreach ($entity
->getTranslationLanguages() as $language) {
$translation = $entity
->getTranslation($language
->getId());
if (!$translation
->isDefaultRevision() && $translation
->isRevisionTranslationAffected()) {
return $translation;
}
}
}
public function hasPendingRevision(ContentEntityInterface $entity) {
$result = FALSE;
if ($this
->isModeratedEntity($entity)) {
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$latest_revision_id = $storage
->getLatestTranslationAffectedRevisionId($entity
->id(), $entity
->language()
->getId());
$default_revision_id = $entity
->isDefaultRevision() && !$entity
->isNewRevision() && ($revision_id = $entity
->getRevisionId()) ? $revision_id : $this
->getDefaultRevisionId($entity
->getEntityTypeId(), $entity
->id());
if ($latest_revision_id !== NULL && $latest_revision_id != $default_revision_id) {
$latest_revision = $storage
->loadRevision($latest_revision_id);
$result = !$latest_revision
->wasDefaultRevision();
}
}
return $result;
}
public function isLiveRevision(ContentEntityInterface $entity) {
$workflow = $this
->getWorkflowForEntity($entity);
return $entity
->isLatestRevision() && $entity
->isDefaultRevision() && $entity->moderation_state->value && $workflow
->getTypePlugin()
->getState($entity->moderation_state->value)
->isPublishedState();
}
public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
$workflow = $this
->getWorkflowForEntity($entity);
$default_revision = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
$default_revision = $default_revision ?: $entity;
if ($default_revision instanceof TranslatableInterface && $default_revision
->isTranslatable()) {
foreach ($default_revision
->getTranslationLanguages() as $language) {
$translation = $default_revision
->getTranslation($language
->getId());
$moderation_state = $translation->moderation_state->value;
if (!$moderation_state) {
continue;
}
if ($workflow
->getTypePlugin()
->getState($moderation_state)
->isPublishedState()) {
return TRUE;
}
}
}
return $workflow
->getTypePlugin()
->getState($default_revision->moderation_state->value)
->isPublishedState();
}
public function getWorkflowForEntity(ContentEntityInterface $entity) {
return $this
->getWorkflowForEntityTypeAndBundle($entity
->getEntityTypeId(), $entity
->bundle());
}
public function getWorkflowForEntityTypeAndBundle($entity_type_id, $bundle_id) {
$bundles = $this->bundleInfo
->getBundleInfo($entity_type_id);
if (isset($bundles[$bundle_id]['workflow'])) {
return $this->entityTypeManager
->getStorage('workflow')
->load($bundles[$bundle_id]['workflow']);
}
return NULL;
}
public function getUnsupportedFeatures(EntityTypeInterface $entity_type) {
$features = [];
if (!$entity_type
->entityClassImplements(EntityPublishedInterface::class)) {
$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(),
]);
}
return $features;
}
public function getOriginalState(ContentEntityInterface $entity) {
$state = NULL;
$workflow_type = $this
->getWorkflowForEntity($entity)
->getTypePlugin();
if (!$entity
->isNew() && !$this
->isFirstTimeModeration($entity)) {
$original_entity = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadRevision($entity
->getLoadedRevisionId());
if (!$entity
->isDefaultTranslation() && $original_entity
->hasTranslation($entity
->language()
->getId())) {
$original_entity = $original_entity
->getTranslation($entity
->language()
->getId());
}
if ($workflow_type
->hasState($original_entity->moderation_state->value)) {
$state = $workflow_type
->getState($original_entity->moderation_state->value);
}
}
return $state ?: $workflow_type
->getInitialState($entity);
}
protected function isFirstTimeModeration(ContentEntityInterface $entity) {
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$original_entity = $storage
->loadRevision($storage
->getLatestRevisionId($entity
->id()));
if ($original_entity) {
$original_id = $original_entity->moderation_state;
}
return !($entity->moderation_state && $original_entity && $original_id);
}
}