You are here

social_swiftmail.module in Open Social 8.7

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;
use Drupal\social_swiftmail\Element\SocialSwiftmailTextFormat;

/**
 * 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;

  // Check if custom e-mail setting for branding removal is enabled.
  $social_swiftmail_config = \Drupal::config('social_swiftmail.settings');
  if ($social_swiftmail_config
    ->get('remove_open_social_branding') === TRUE) {
    $site_config = \Drupal::config('system.site');

    // When branding should be removed, check if the default site settings are
    // set and override variables.
    if ($site_config) {
      $variables['site_link'] = TRUE;
      $variables['site_name'] = $site_config
        ->get('name');
      if ($site_config
        ->get('slogan')) {
        $variables['site_slogan'] = $site_config
          ->get('slogan');
      }
    }
  }
  else {
    $variables['site_name'] = t('Open Social');
    $variables['site_slogan'] = '"' . t('Make your people bloom') . '"';
  }

  // Check if a custom e-mail header is set and apply the configuration
  // to the render array.
  if (($header_config = $social_swiftmail_config
    ->get('template_header')) && (!empty($header_config['value']) && !empty($header_config['format']))) {
    $header_markup = [
      '#type' => 'processed_text',
      '#text' => $header_config['value'],
      '#format' => $header_config['format'],
    ];
    $variables['header'] = \Drupal::service('renderer')
      ->renderRoot($header_markup);
  }

  // Check if a custom e-mail footer is set and apply the configuration
  // to the render array.
  if (($footer_config = $social_swiftmail_config
    ->get('template_footer')) && (!empty($footer_config['value']) && !empty($footer_config['format']))) {
    $footer_markup = [
      '#type' => 'processed_text',
      '#text' => $footer_config['value'],
      '#format' => $footer_config['format'],
    ];
    $variables['footer'] = \Drupal::service('renderer')
      ->renderRoot($footer_markup);
  }
  $message =& $variables['message'];
  $user = user_load_by_mail($message['to']);

  // Try to add a heading message.
  if ($user || !empty($message['params']['context']['display_name'])) {
    $options = [
      'langcode' => $message['langcode'],
    ];
    if ($user) {
      $display_name = $user
        ->getDisplayName();
    }
    else {
      $display_name = $message['params']['context']['display_name'];
    }
    $replace = [
      '@display_name' => $display_name,
    ];
    $variables['heading'] = t('Hi <strong>@display_name</strong>', $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) {
  global $base_url;

  // Default we use the logo image.
  $logo = $base_url . 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;
}

/**
 * Implements hook_element_info_alter().
 */
function social_swiftmail_element_info_alter(array &$info) {
  if (isset($info['text_format'])) {
    if (!isset($info['text_format']['#process'])) {
      $info['text_format']['#process'] = [];
    }
    $info['text_format']['#process'][] = [
      SocialSwiftmailTextFormat::class,
      'process',
    ];
  }
}

Functions