You are here

social_swiftmail.module in Open Social 8

Module file for Social Swiftmailer.

File

modules/social_features/social_swiftmail/social_swiftmail.module
View source
<?php

/**
 * @file
 * Module file for  Social Swiftmailer.
 */
use Drupal\Component\Utility\Xss;
use Drupal\file\Entity\File;

/**
 * Preprocess swift template.
 */
function social_swiftmail_preprocess_swiftmailer(array &$variables) {

  // Load default theme (not active).
  $theme_id = \Drupal::config('system.theme')
    ->get('default');

  // Need to check this, since otherwise site-install will fail.
  if (\Drupal::service('module_handler')
    ->moduleExists('color')) {

    // Load the colors.
    $colors = color_get_palette($theme_id);
  }

  // Set variables from theme.
  $logo = social_swiftmail_get_logo($theme_id);
  $primary = $colors['brand-primary'];
  $secondary = $colors['brand-secondary'];
  $accent = $colors['brand-accent'];
  $link = $colors['brand-link'];
  $border_radius = Xss::filter(theme_get_setting('border_radius', $theme_id));

  // Add variables to send to the html template.
  $variables['logo'] = $logo;
  $variables['primary'] = $primary;
  $variables['secondary'] = $secondary;
  $variables['accent'] = $accent;
  $variables['link'] = $link;
  $variables['border_radius'] = $border_radius;

  // Try to add a heading message.
  if ($user = user_load_by_mail($variables['message']['to'])) {
    $message = $variables['message'];
    $options = [
      'langcode' => $message['langcode'],
    ];
    $replace = [
      '%display_name' => $user
        ->getDisplayName(),
    ];
    $variables['heading'] = t('Hi %display_name', $replace, $options);
  }
}

/**
 * Implements hook_mail_alter().
 */
function social_swiftmail_mail_alter(&$message) {

  // Get the site settings.
  $site_settings = \Drupal::config('system.site');

  // If there is something set as 'from' sender, we append the site name.
  if (isset($message['from'])) {
    $message['from'] = $site_settings
      ->get('name') . ' <' . $message['from'] . '>';
  }
  else {
    $message['from'] = $site_settings
      ->get('name') . ' <' . $site_settings
      ->get('mail') . '>';
  }
}

/**
 * Function the fetches the logo.
 */
function social_swiftmail_get_logo($theme_id) {

  // Determine host.
  $request = \Drupal::request();
  $host = $request
    ->getSchemeAndHttpHost();

  // Default we use the logo image.
  $logo = $host . theme_get_setting('logo.url', $theme_id);

  // It could be overridden here.
  $email_logo = theme_get_setting('email_logo', $theme_id);

  // Must be a non-empty array.
  if (is_array($email_logo) && !empty($email_logo)) {
    $file = File::load($email_logo[0]);
    if ($file instanceof File) {
      $logo = file_create_url($file
        ->getFileUri());
    }
  }
  return $logo;
}

Functions

Namesort descending Description
social_swiftmail_get_logo Function the fetches the logo.
social_swiftmail_mail_alter Implements hook_mail_alter().
social_swiftmail_preprocess_swiftmailer Preprocess swift template.