You are here

content_moderation_notifications.module in Content Moderation Notifications 8.2

Hook implementations for the content moderation notifications module.

File

content_moderation_notifications.module
View source
<?php

/**
 * @file
 * Hook implementations for the content moderation notifications module.
 */
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_entity_presave().
 */
function content_moderation_notifications_entity_presave(EntityInterface $entity) {

  // Attach the last revision of the entity.
  // It's important to attach the revision in hook_entity_presave() as the same
  // action in hook_entity_update() will result a different revision being
  // in transaction.
  _content_moderation_notifications_ensure_revision($entity);
}

/**
 * Implements hook_entity_update().
 */
function content_moderation_notifications_entity_update(EntityInterface $entity) {
  _content_moderation_notifications_check($entity);
}

/**
 * Implements hook_entity_update().
 */
function content_moderation_notifications_entity_insert(EntityInterface $entity) {
  _content_moderation_notifications_check($entity);
}

/**
 * Implements hook_mail().
 */
function content_moderation_notifications_mail($key, &$message, $params) {
  $options = [
    'langcode' => $message['langcode'],
  ];
  switch ($key) {
    case 'content_moderation_notification':
      $message['from'] = \Drupal::config('system.site')
        ->get('mail');
      $token_service = \Drupal::token();
      $context = $params['context'];
      $subject = PlainTextOutput::renderFromHtml($token_service
        ->replace($params['subject'], $context));
      $body = $token_service
        ->replace($params['message'], $context);
      $message['subject'] = str_replace([
        "\r",
        "\n",
      ], '', $subject);
      $message['body'][] = $body;
      break;
  }
}

/**
 * Triggers the content moderation notification system.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 */
function _content_moderation_notifications_check(EntityInterface $entity) {

  /** @var \Drupal\content_moderation_notifications\NotificationInformationInterface $notification_info */
  $notification_info = Drupal::service('content_moderation_notifications.notification_information');
  $notifications = $notification_info
    ->getNotifications($entity);
  if (!empty($notifications['notifications'])) {

    /** @var \Drupal\content_moderation_notifications\NotificationInterface $notification */
    $notification = Drupal::service('content_moderation_notifications.notification');
    $notification
      ->sendNotification($notifications['entity'], $notifications['notifications']);
  }
}

/**
 * Attach the last untouched revision to the entity.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 */
function _content_moderation_notifications_ensure_revision(EntityInterface $entity) {
  $notification_info = Drupal::service('content_moderation_notifications.notification_information');
  if ($notification_info
    ->isModeratedEntity($entity) && !isset($entity->last_revision)) {
    $entity->last_revision = $notification_info
      ->getLatestRevision($entity
      ->getEntityTypeId(), $entity
      ->id());
  }
}