Commit 892917d5 authored by Rodrigo  Aguilera's avatar Rodrigo Aguilera Committed by Ev Maslovskiy
Browse files

Issue #3254419 by rodrigoaguilera: Add ability to display emails instead of sending them

parent e2ac60b1
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.4]
- Add ability to display emails instead of sending them. Disabled by default.

## [2.0.3]
- No code changes - just not forgetting to write changelog records!

+1 −0
Original line number Diff line number Diff line
@@ -8,3 +8,4 @@ test_users_email_suffix: ''
test_users_password: ''
test_users_password_for_production: ''
test_users_usernames_map: {  }
display_emails_as_messages: 0
+37 −0
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@
 * Hook implementations for the Domino module.
 */

use Drupal\domino\ApplicationInterface;
use Drupal\Core\Render\Markup;

/**
 * Implements hook_cache_flush().
 */
@@ -24,3 +27,37 @@ function domino_cache_flush() {
  // matches the desired state.
  $test_users->ensureTestUsersActivationStatus();
}

/**
 * Implements hook_mail_alter().
 *
 * Display the email about to be sent.
 */
function domino_mail_alter(&$message) {
  $config = \Drupal::config('domino.settings');
  if ($config->get('application_mode') === ApplicationInterface::MODE_PRODUCTION || !$config->get('display_emails_as_messages')) {
    return;
  }
  if (isset($message['body'][0])) {
    $message['send'] = FALSE;
    // Linkify URLs from:
    // https://stackoverflow.com/questions/1960461/convert-plain-text-urls-into-html-hyperlinks-in-php
    $url = '@(http(s)?)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
    $bodyWithLinks = preg_replace($url, '<a href="$0" class="email-link" title="$0">$0</a>', $message['body'][0]);
    $messageBody = Markup::create($bodyWithLinks);
    \Drupal::messenger()->addStatus($messageBody);
  }
}

/**
 * Implements hook_module_implements_alter().
 *
 * The hook to display messages needs to happen last.
 */
function domino_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'mail_alter') {
    $group = $implementations['domino'];
    unset($implementations['domino']);
    $implementations['domino'] = $group;
  }
}