You are here

content_moderation_notifications.module in Content Moderation Notifications 8.3

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\content_moderation_notifications\ContentModerationNotificationInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * 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) {
  \Drupal::service('content_moderation_notifications.notification')
    ->processEntity($entity);
}

/**
 * Implements hook_entity_insert().
 */
function content_moderation_notifications_entity_insert(EntityInterface $entity) {
  \Drupal::service('content_moderation_notifications.notification')
    ->processEntity($entity);
}

/**
 * Implements hook_mail().
 */
function content_moderation_notifications_mail($key, &$message, $params) {
  switch ($key) {
    case 'content_moderation_notification':

      // Add headers, specifically this should include the Bcc header.
      if (!empty($params['headers'])) {
        foreach ($params['headers'] as $id => $value) {
          $message['headers'][$id] = $params['headers'][$id];
        }
      }
      $message['from'] = \Drupal::config('system.site')
        ->get('mail');
      $token_service = \Drupal::token();
      $context = $params['context'];
      $subject = PlainTextOutput::renderFromHtml($token_service
        ->replace($params['subject'], $context, [
        'clear' => TRUE,
      ]));
      $body = $token_service
        ->replace($params['message'], $context, [
        'clear' => TRUE,
      ]);
      $message['subject'] = str_replace([
        "\r",
        "\n",
      ], '', $subject);
      $message['body'][] = Markup::create($body);
      break;
  }
}

/**
 * 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());
  }
}

/**
 * Implements hook_entity_operation().
 */
function content_moderation_notifications_entity_operation(EntityInterface $entity) {
  $operations = [];
  if ($entity instanceof ContentModerationNotificationInterface) {
    if ($entity
      ->status()) {
      $operations['disable'] = [
        'title' => t('Disable'),
        'url' => $entity
          ->toUrl('disable-form'),
        'weight' => 50,
      ];
    }
    else {
      $operations['enable'] = [
        'title' => t('Enable'),
        'url' => $entity
          ->toUrl('enable-form'),
        'weight' => 50,
      ];
    }
  }
  return $operations;
}

/**
 * Implements hook_help().
 */
function content_moderation_notifications_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.content_moderation_notifications':
      $output = '';
      $output .= '<h3>' . t("Introduction") . '</h3>';
      $output .= '<p>' . t("The Content Moderation Notifications module allows\n        notifications to be sent to all users of a particular role, or to the\n        content's author when a piece of content is transitioned from one state\n        to another via core's Content Moderation module.") . '</p>';
      return $output;
  }
}