3 namespace Drupal\content_moderation;
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;
12 * General service for moderation-related questions about Entity API.
14 class ModerationInformation implements ModerationInformationInterface {
17 * The entity type manager.
19 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21 protected $entityTypeManager;
24 * The bundle information service.
26 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
28 protected $bundleInfo;
31 * Creates a new ModerationInformation instance.
33 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
34 * The entity type manager.
35 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
36 * The bundle information service.
38 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
39 $this->entityTypeManager = $entity_type_manager;
40 $this->bundleInfo = $bundle_info;
46 public function isModeratedEntity(EntityInterface $entity) {
47 if (!$entity instanceof ContentEntityInterface) {
51 return $this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle());
57 public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
58 return $entity_type->hasHandlerClass('moderation');
64 public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
65 if ($this->canModerateEntitiesOfEntityType($entity_type)) {
66 $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
67 return isset($bundles[$bundle]['workflow']);
75 public function getLatestRevision($entity_type_id, $entity_id) {
76 if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) {
77 return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id);
84 public function getLatestRevisionId($entity_type_id, $entity_id) {
85 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
86 $revision_ids = $storage->getQuery()
88 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
89 ->sort($this->entityTypeManager->getDefinition($entity_type_id)->getKey('revision'), 'DESC')
93 return array_keys($revision_ids)[0];
101 public function getDefaultRevisionId($entity_type_id, $entity_id) {
102 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
103 $revision_ids = $storage->getQuery()
104 ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
105 ->sort($this->entityTypeManager->getDefinition($entity_type_id)->getKey('revision'), 'DESC')
109 return array_keys($revision_ids)[0];
117 public function isLatestRevision(ContentEntityInterface $entity) {
118 return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
124 public function hasForwardRevision(ContentEntityInterface $entity) {
125 return $this->isModeratedEntity($entity)
126 && !($this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()) == $this->getDefaultRevisionId($entity->getEntityTypeId(), $entity->id()));
132 public function isLiveRevision(ContentEntityInterface $entity) {
133 $workflow = $this->getWorkflowForEntity($entity);
134 return $this->isLatestRevision($entity)
135 && $entity->isDefaultRevision()
136 && $entity->moderation_state->value
137 && $workflow->getState($entity->moderation_state->value)->isPublishedState();
143 public function getWorkflowForEntity(ContentEntityInterface $entity) {
144 $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
145 if (isset($bundles[$entity->bundle()]['workflow'])) {
146 return $this->entityTypeManager->getStorage('workflow')->load($bundles[$entity->bundle()]['workflow']);