You are here

function courier_system_mail_alter in Courier 8

Same name and namespace in other branches
  1. 2.x courier_system/courier_system.module \courier_system_mail_alter()

Implements hook_mail_alter().

File

courier_system/courier_system.module, line 32

Code

function courier_system_mail_alter(&$message) {
  $mail_id = $message['id'];
  $settings = \Drupal::config('courier_system.settings');
  $override = $settings
    ->get('override');
  if (!empty($override[$mail_id])) {

    // Cancel sending.
    $message['send'] = FALSE;
    $gtc = GlobalTemplateCollection::load('courier_system.' . $mail_id);
    if ($gtc) {
      $template_collection = $gtc
        ->getTemplateCollection();

      // _user_mail_notify() adds account param.
      // Only valid for user.module mails.

      /** @var \Drupal\Core\Session\AccountInterface $account */
      $account = $message['params']['account'];

      /** @var User $identity */
      $identity = User::load($account
        ->id());
      $template_collection
        ->setTokenValue('user', $identity)
        ->setTokenOption('callback', 'user_mail_tokens');

      /** @var \Drupal\courier\Service\CourierManagerInterface $courier_manager */
      $courier_manager = \Drupal::service('courier.manager');
      $courier_manager
        ->sendMessage($template_collection, $identity);
    }
  }
}