You are here

public function PrivateMessageNotifier::notify in Private Message 8.2

Send a private message notification email.

Parameters

\Drupal\private_message\Entity\PrivateMessageInterface $message: The message.

\Drupal\private_message\Entity\PrivateMessageThreadInterface $thread: The message thread.

\Drupal\user\UserInterface[] $members: The message members.

Overrides PrivateMessageNotifierInterface::notify

File

src/Service/PrivateMessageNotifier.php, line 96

Class

PrivateMessageNotifier
A service class for sending notifications of private messages.

Namespace

Drupal\private_message\Service

Code

public function notify(PrivateMessageInterface $message, PrivateMessageThreadInterface $thread, array $members = []) {
  foreach ($members as $member) {
    if ($member
      ->id() != $this->currentUser
      ->id()) {

      // Check if the notification should be sent.
      if ($this
        ->shouldSend($member, $message, $thread)) {

        // Send the notification.
        // This is done through integration with the Message module, by
        // creating a new Message entity.
        $notification = $this->messageManager
          ->create([
          'template' => 'private_message_notification',
          'uid' => $member
            ->id(),
        ]);
        $notification
          ->set('field_message_private_message', $message);
        $notification
          ->set('field_message_pm_thread', $thread);
        $notification
          ->save();
        $this->messageNotifier
          ->send($notification);
      }
    }
  }
}