You are here

class NotificationInformation in Content Moderation Notifications 8.2

Same name and namespace in other branches
  1. 8.3 src/NotificationInformation.php \Drupal\content_moderation_notifications\NotificationInformation

Service for notification related questions about the moderated entity.

Hierarchy

Expanded class hierarchy of NotificationInformation

1 string reference to 'NotificationInformation'
content_moderation_notifications.services.yml in ./content_moderation_notifications.services.yml
content_moderation_notifications.services.yml
1 service uses NotificationInformation
content_moderation_notifications.notification_information in ./content_moderation_notifications.services.yml
Drupal\content_moderation_notifications\NotificationInformation

File

src/NotificationInformation.php, line 12

Namespace

Drupal\content_moderation_notifications
View source
class NotificationInformation implements NotificationInformationInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * General service for moderation-related questions about Entity API.
   *
   * @var \Drupal\content_moderation\ModerationInformation
   */
  protected $moderationInformation;

  /**
   * Creates a new NotificationInformation instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
   *   The bundle information service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moderationInformation = $moderation_information;
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratedEntity(EntityInterface $entity) {
    return $this->moderationInformation
      ->isModeratedEntity($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function getWorkflow(EntityInterface $entity) {
    return $this
      ->isModeratedEntity($entity) ? $this->moderationInformation
      ->getWorkflowForEntity($entity) : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getNotifications(EntityInterface $entity) {
    $notifications = [
      'entity' => $entity,
      'notifications' => [],
    ];
    if ($this
      ->isModeratedEntity($entity)) {
      $workflow = $this
        ->getWorkflow($entity);
      $transition = $this
        ->getTransition($entity);

      // Find out if we have a config entity that contains this transition.
      $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;
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestRevision($entity_type_id, $entity_id) {
    return $this->moderationInformation
      ->getLatestRevision($entity_type_id, $entity_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NotificationInformation::$entityTypeManager protected property The entity type manager.
NotificationInformation::$moderationInformation protected property General service for moderation-related questions about Entity API.
NotificationInformation::getLatestRevision public function Loads the latest revision of a specific entity. Overrides NotificationInformationInterface::getLatestRevision
NotificationInformation::getNotifications public function Gets the list of notification based on the current transition. Overrides NotificationInformationInterface::getNotifications
NotificationInformation::getTransition public function Checks for the current transition of the moderated entity. Overrides NotificationInformationInterface::getTransition
NotificationInformation::getWorkflow public function Checks for the workflow object of the moderated entity. Overrides NotificationInformationInterface::getWorkflow
NotificationInformation::isModeratedEntity public function Determines if an entity is moderated. Overrides NotificationInformationInterface::isModeratedEntity
NotificationInformation::__construct public function Creates a new NotificationInformation instance.