You are here

function content_moderation_notifications_entity_update in Content Moderation Notifications 8

Same name and namespace in other branches
  1. 8.3 content_moderation_notifications.module \content_moderation_notifications_entity_update()
  2. 8.2 content_moderation_notifications.module \content_moderation_notifications_entity_update()

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

File

./content_moderation_notifications.module, line 6

Code

function content_moderation_notifications_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  $mailManager = \Drupal::service('plugin.manager.mail');
  $langcode = \Drupal::currentUser()
    ->getPreferredLangcode();

  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = Drupal::service('content_moderation.moderation_information');

  // Check to see if the content is moderated or not
  $is_moderated = $moderation_info
    ->isModeratedEntity($entity);
  if ($is_moderated) {

    // Get the previous and current states of the content.
    $previous_state = $entity->original->moderation_state->target_id;
    $current_state = $entity->moderation_state->target_id;

    // use the current and previous state to find any matching transitions.
    $transitions = Drupal::service('entity_type.manager')
      ->getStorage('moderation_state_transition')
      ->loadMultiple();

    // Loop through all the possible transitions.
    foreach ($transitions as $key => $transition) {
      if ($transition
        ->getFromState() == $previous_state && $transition
        ->getToState() == $current_state) {

        // Find out if we have a config entity that contains this transition.
        $query = \Drupal::entityQuery('content_moderation_notification')
          ->condition('transitions', $transition
          ->id(), 'IN');
        $notification_ids = $query
          ->execute();
        $notifications = Drupal::service('entity_type.manager')
          ->getStorage('content_moderation_notification')
          ->loadMultiple($notification_ids);
        foreach ($notifications as $notification) {

          // Setup the email subject and body content.
          $params['subject'] = $notification->subject;
          $params['message'] = check_markup($notification->body['value'], $notification->body['format']);

          // Figure out who the email should be going to.
          $to = array();

          // Authors.
          if ($notification->author) {
            $to[] = $entity
              ->getOwner()->mail->value;
          }

          // Roles.
          $roles = array_keys(array_filter($notification->roles));
          foreach ($roles as $role) {
            $role_users = \Drupal::service('entity_type.manager')
              ->getStorage('user')
              ->loadByProperties([
              'roles' => $role,
            ]);
            foreach ($role_users as $role_user) {
              $to[] = $role_user->mail->value;
            }
          }

          // Adhoc emails.
          $adhoc_emails = array_map('trim', explode(',', $notification->emails));
          foreach ($adhoc_emails as $email) {
            $to[] = $email;
          }

          // Remove any null values that have crept in.
          $to = array_filter($to);

          // Remove any duplicates.
          $to = array_unique($to);
          $result = $mailManager
            ->mail('content_moderation_notifications', 'content_moderation_notification', implode(',', $to), $langcode, $params, NULL, true);
          if ($result['result'] !== true) {
            drupal_set_message(t('There was a problem sending the notification email.'), 'error');
          }
        }
      }
    }
  }
}