You are here

varbase_email.module in Varbase Email 9.0.x

Same filename and directory in other branches
  1. 8.6 varbase_email.module

File

varbase_email.module
View source
<?php

/**
 * @file
 * Contains varbase_email.module.
 */
use Drupal\file\Entity\File;
use Drupal\Core\StreamWrapper\StreamWrapperManager;

/**
 * Implements hook_theme().
 */
function varbase_email_theme($existing, $type, $theme, $path) {
  $templates = $path . '/templates';
  $return['swiftmailer'] = [
    'template' => 'varbase_emails',
    'path' => $templates,
    'variables' => [
      'message' => [],
    ],
    'mail theme' => TRUE,
  ];
  return $return;
}

/**
 * Prepares variables for varbase_emails.html.twig templates.
 *
 * Implements hook_preprocess_HOOK() for field templates.
 */
function varbase_email_preprocess_swiftmailer(&$variables) {
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $theme_id = \Drupal::config('system.theme')
    ->get('default');
  $site_config = \Drupal::config('system.site');
  $request = \Drupal::request();
  $host = $request
    ->getSchemeAndHttpHost();
  $variables['dir'] = $language
    ->getDirection();

  // Default we use the logo image.
  if (theme_get_setting('email_logo_default', $theme_id)) {
    $variables['logo'] = $host . theme_get_setting('logo.url', $theme_id);
  }
  else {
    $fid = theme_get_setting('email_logo_upload', $theme_id);
    if ($fid && is_array($fid) && count($fid)) {
      $file = File::load($fid[0]);
      if ($file) {
        $url = $file
          ->createFileUrl();
        $variables['logo'] = $url;
      }
    }
    elseif (theme_get_setting('email_logo_path', $theme_id)) {
      $uri = theme_get_setting('email_logo_path', $theme_id);
      $scheme = StreamWrapperManager::getScheme($uri);
      if ($scheme) {
        $variables['logo'] = file_create_url($uri);
      }
      else {
        $variables['logo'] = $host . file_create_url($uri);
      }
    }
    else {
      $variables['logo'] = $host . theme_get_setting('logo.url', $theme_id);
    }
  }
  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('Varbase');
    $variables['site_slogan'] = '"' . t('The Ultimate Drupal 8 CMS Starter Kit') . '"';
  }
  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);
  }
}

Functions

Namesort descending Description
varbase_email_preprocess_swiftmailer Prepares variables for varbase_emails.html.twig templates.
varbase_email_theme Implements hook_theme().