View source
<?php
namespace Drupal\mailsystem;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Mail\MailManager;
use Drupal\Core\Theme\ThemeInitializationInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
class MailsystemManager extends MailManager {
const MAILSYSTEM_TYPE_SENDING = 'sender';
const MAILSYSTEM_TYPE_FORMATTING = 'formatter';
const MAILSYSTEM_MODULES_CONFIG = 'modules';
protected $themeManager;
protected $themeInitialization;
public function setThemeManager(ThemeManagerInterface $theme_manager) {
$this->themeManager = $theme_manager;
}
public function setThemeInitialization(ThemeInitializationInterface $theme_initialization) {
$this->themeInitialization = $theme_initialization;
}
public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
$mail_theme = $this
->getMailTheme();
$current_active_theme = $this->themeManager
->getActiveTheme();
if ($mail_theme && $mail_theme != $current_active_theme
->getName()) {
$this->themeManager
->setActiveTheme($this->themeInitialization
->initTheme($mail_theme));
}
try {
$message = parent::mail($module, $key, $to, $langcode, $params, $reply, $send);
} finally {
if ($mail_theme != $current_active_theme
->getName()) {
$this->themeManager
->setActiveTheme($current_active_theme);
}
}
return $message;
}
public function getInstance(array $options) {
$module = isset($options['module']) ? $options['module'] : 'default';
$key = isset($options['key']) ? $options['key'] : '';
return new Adapter($this
->getPluginInstance($module, $key, static::MAILSYSTEM_TYPE_FORMATTING), $this
->getPluginInstance($module, $key, static::MAILSYSTEM_TYPE_SENDING));
}
protected function getPluginInstance($module, $key = '', $type = '') {
$plugin_id = NULL;
$message_id_list = [
self::MAILSYSTEM_MODULES_CONFIG . '.' . $module . '.' . $key . '.' . $type,
self::MAILSYSTEM_MODULES_CONFIG . '.' . $module . '.none.' . $type,
self::MAILSYSTEM_MODULES_CONFIG . '.' . $module . '.' . $type,
'defaults.' . $type,
'defaults',
];
$config = $this->configFactory
->get('mailsystem.settings');
foreach ($message_id_list as $message_id) {
$plugin_id = $config
->get($message_id);
if (!is_null($plugin_id)) {
break;
}
}
if (empty($this->instances[$plugin_id])) {
$plugin = $this
->createInstance($plugin_id);
if ($plugin instanceof MailInterface) {
$this->instances[$plugin_id] = $plugin;
}
else {
throw new InvalidPluginDefinitionException($plugin_id, new FormattableMarkup('Class %class does not implement interface %interface', [
'%class' => get_class($plugin),
'%interface' => 'Drupal\\Core\\Mail\\MailInterface',
]));
}
}
return $this->instances[$plugin_id];
}
public function getMailTheme() {
$theme = $this->configFactory
->get('mailsystem.settings')
->get('theme');
switch ($theme) {
case 'default':
$theme = $this->configFactory
->get('system.theme')
->get('default');
break;
case 'current':
$theme = $this->themeManager
->getActiveTheme()
->getName();
break;
case 'domain':
if (FALSE && \Drupal::moduleHandler()
->moduleExists('domain_theme')) {
global $_domain;
$domain_theme = domain_theme_lookup($_domain['domain_id']);
$theme = $domain_theme != -1 ? $domain_theme['theme'] : $this->themeManager
->getActiveTheme()
->getName();
}
break;
}
return $theme;
}
}