View source  
  <?php
namespace Drupal\content_moderation_notifications;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\token\TokenEntityMapperInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\RoleInterface;
class Notification implements NotificationInterface {
  
  protected $currentUser;
  
  protected $entityTypeManager;
  
  protected $mailManager;
  
  protected $moduleHandler;
  
  protected $notificationInformation;
  
  protected $tokenEntityMapper;
  
  public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, MailManagerInterface $mail_manager, ModuleHandlerInterface $module_handler, NotificationInformationInterface $notification_information, TokenEntityMapperInterface $token_entity_mappper = NULL) {
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
    $this->mailManager = $mail_manager;
    $this->moduleHandler = $module_handler;
    $this->notificationInformation = $notification_information;
    $this->tokenEntityMapper = $token_entity_mappper;
  }
  
  public function processEntity(EntityInterface $entity) {
    $notifications = $this->notificationInformation
      ->getNotifications($entity);
    if (!empty($notifications)) {
      $this
        ->sendNotification($entity, $notifications);
    }
  }
  
  public function sendNotification(EntityInterface $entity, array $notifications) {
    
    foreach ($notifications as $notification) {
      $data['langcode'] = $this->currentUser
        ->getPreferredLangcode();
      $data['notification'] = $notification;
      
      $data['params']['subject'] = $notification
        ->getSubject();
      $data['params']['message'] = check_markup($notification
        ->getMessage(), $notification
        ->getMessageFormat());
      
      if ($this->tokenEntityMapper) {
        $data['params']['context'] = [
          'entity' => $entity,
          'user' => $this->currentUser,
          $this->tokenEntityMapper
            ->getTokenTypeForEntityType($entity
            ->getEntityTypeId()) => $entity,
        ];
      }
      else {
        $data['params']['context'] = [
          'entity' => $entity,
          'user' => $this->currentUser,
          $entity
            ->getEntityTypeId() => $entity,
        ];
      }
      
      $data['to'] = [];
      
      if ($notification->author and $entity instanceof EntityOwnerInterface) {
        $data['to'][] = $entity
          ->getOwner()
          ->getEmail();
      }
      
      foreach ($notification
        ->getRoleIds() as $role) {
        
        $user_storage = $this->entityTypeManager
          ->getStorage('user');
        if ($role === RoleInterface::AUTHENTICATED_ID) {
          $uids = \Drupal::entityQuery('user')
            ->condition('status', 1)
            ->accessCheck(FALSE)
            ->execute();
          
          $role_users = $user_storage
            ->loadMultiple(array_filter($uids));
        }
        else {
          
          $role_users = $user_storage
            ->loadByProperties([
            'roles' => $role,
          ]);
        }
        foreach ($role_users as $role_user) {
          $data['to'][] = $role_user
            ->getEmail();
        }
      }
      
      $adhoc_emails = array_map('trim', explode(',', preg_replace("/((\r?\n)|(\r\n?))/", ',', $notification
        ->getEmails())));
      foreach ($adhoc_emails as $email) {
        $data['to'][] = $email;
      }
      
      $this->moduleHandler
        ->alter('content_moderation_notification_mail_data', $entity, $data);
      
      $data['to'] = array_filter($data['to']);
      
      $data['to'] = array_unique($data['to']);
      
      $data['params']['headers']['Bcc'] = implode(',', $data['to']);
      $recipient = '';
      if (!$notification
        ->disableSiteMail()) {
        $recipient = \Drupal::config('system.site')
          ->get('mail');
      }
      if (!empty($data['params']['headers']['Bcc'])) {
        $this->mailManager
          ->mail('content_moderation_notifications', 'content_moderation_notification', $recipient, $data['langcode'], $data['params'], NULL, TRUE);
      }
    }
  }
}