degov-8.x-2.0/modules/degov_html_mail/degov_html_mail.module
modules/degov_html_mail/degov_html_mail.module
<?php
use Drupal\degov_common\Common;
/**
* Implements hook_preprocess().
*/
function degov_html_mail_preprocess(&$variables, $hook, &$info) {
// Add template suggestions and libraries implemented in this module.
Common::addThemeSuggestions($variables, $hook, $info, [
'module_name' => 'degov_html_mail',
'entity_type' => 'swiftmailer',
'entity_bundles' => [],
'entity_view_modes' => [],
]);
if ($hook == 'swiftmailer') {
/** @var \Drupal\Core\Theme\ActiveTheme $theme */
$theme = \Drupal::service('theme.manager')->getActiveTheme();
// Check if there is mail.css exists in current active theme.
$mailstyle = DRUPAL_ROOT . '/'. drupal_get_path('theme', $theme->getName()) . '/public/css/mail.css';
if (is_file($mailstyle)) {
// If yes include the content of the file into email message.
$variables['css'] = @file_get_contents($mailstyle);
}
$variables['base_url'] = $GLOBALS['base_url'] . '/';
}
}
/**
* Changes the relative links in the email to absolute.
*
* Implements hook_mail_alter().
*/
function degov_html_mail_mail_alter(&$message) {
// Attempt to convert relative URLs to absolute. Only apply this to parts
// of the body which are markup objects.
// Get Host of the site to have the links to start with it.
$host = \Drupal::request()->getSchemeAndHttpHost();
foreach ($message['body'] as &$body_part) {
if ($body_part instanceof \Drupal\Component\Render\MarkupInterface) {
$convertedString = \Drupal\Component\Utility\Html::transformRootRelativeUrlsToAbsolute((string) $body_part, $host);
$body_part = \Drupal\Core\Render\Markup::create($convertedString);
}
}
}