You are here

class Notification in Content Moderation Notifications 8.2

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

General service for moderation-related questions about Entity API.

Hierarchy

Expanded class hierarchy of Notification

1 string reference to 'Notification'
content_moderation_notifications.services.yml in ./content_moderation_notifications.services.yml
content_moderation_notifications.services.yml
1 service uses Notification
content_moderation_notifications.notification in ./content_moderation_notifications.services.yml
Drupal\content_moderation_notifications\Notification

File

src/Notification.php, line 11

Namespace

Drupal\content_moderation_notifications
View source
class Notification implements NotificationInterface {

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

  /**
   * Creates a new ModerationInformation instance.
   *
   * @param \Drupal\Core\Mail\MailManager $mail_manager
   *   The mail manager.
   */
  public function __construct(MailManager $mail_manager) {
    $this->mailManager = $mail_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function sendNotification(EntityInterface $entity, array $notifications) {
    foreach ($notifications as $notification) {
      $data['langcode'] = \Drupal::currentUser()
        ->getPreferredLangcode();
      $data['notification'] = $notification;

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

      // Add the entity as context to aid in token replacement.
      $data['params']['context'] = [
        'entity' => $entity,
        'user' => \Drupal::currentUser(),
      ];

      // Figure out who the email should be going to.
      $data['to'] = [];

      // Authors.
      if ($notification->author) {
        $data['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) {
          $data['to'][] = $role_user->mail->value;
        }
      }

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

      // Let other modules to alter the email data.
      \Drupal::moduleHandler()
        ->alter('content_moderation_notification_mail_data', $entity, $data);

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

      // Remove any duplicates.
      $data['to'] = array_unique($data['to']);
      $this->mailManager
        ->mail('content_moderation_notifications', 'content_moderation_notification', implode(',', $data['to']), $data['langcode'], $data['params'], NULL, TRUE);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Notification::$mailManager protected property The entity type manager.
Notification::sendNotification public function Send notifications for a given entity and set of notifications. Overrides NotificationInterface::sendNotification
Notification::__construct public function Creates a new ModerationInformation instance.