NotificationInformation.php in Content Moderation Notifications 8.2
File
src/NotificationInformation.php
View source
<?php
namespace Drupal\content_moderation_notifications;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\content_moderation\ModerationInformationInterface;
class NotificationInformation implements NotificationInformationInterface {
protected $entityTypeManager;
protected $moderationInformation;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
$this->entityTypeManager = $entity_type_manager;
$this->moderationInformation = $moderation_information;
}
public function isModeratedEntity(EntityInterface $entity) {
return $this->moderationInformation
->isModeratedEntity($entity);
}
public function getWorkflow(EntityInterface $entity) {
return $this
->isModeratedEntity($entity) ? $this->moderationInformation
->getWorkflowForEntity($entity) : FALSE;
}
public function getTransition(EntityInterface $entity) {
$transition = FALSE;
if ($workflow = $this
->getWorkflow($entity)) {
$current_state = $entity->moderation_state->value;
$previous_state = '';
if (isset($entity->last_revision)) {
$previous_state = $entity->last_revision->moderation_state->value;
}
if (empty($previous_state)) {
$previous_state = $workflow
->getTypePlugin()
->getInitialState($workflow, $entity)
->id();
}
$transition = $workflow
->getTransitionFromStateToState($previous_state, $current_state);
}
return $transition;
}
public function getNotifications(EntityInterface $entity) {
$notifications = [
'entity' => $entity,
'notifications' => [],
];
if ($this
->isModeratedEntity($entity)) {
$workflow = $this
->getWorkflow($entity);
$transition = $this
->getTransition($entity);
$query = \Drupal::entityQuery('content_moderation_notification')
->condition('workflow', $workflow
->id())
->condition('status', 1)
->condition('transitions.' . $transition
->id(), $transition
->id());
$notification_ids = $query
->execute();
$notifications['notifications'] = !empty($notification_ids) ? $this->entityTypeManager
->getStorage('content_moderation_notification')
->loadMultiple($notification_ids) : [];
}
return $notifications;
}
public function getLatestRevision($entity_type_id, $entity_id) {
return $this->moderationInformation
->getLatestRevision($entity_type_id, $entity_id);
}
}