Commit 922e4a76 authored by Balis Matzouranis's avatar Balis Matzouranis Committed by Adam Shepherd
Browse files

Issue #3254125 by balis_m, AdamPS, attisan: Selecting default mail theme / enforcing mail theme

parent 5c471c1c
Loading
Loading
Loading
Loading
+8 −0
Changes for config/schema/symfony_mailer.schema.yml: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -93,6 +93,14 @@ symfony_mailer.email_adjuster_plugin.email_subject:
      type: label
      label: 'Email subject'

symfony_mailer.email_adjuster_plugin.email_theme:
  type: mapping
  label: 'Theme'
  mapping:
    theme:
      type: label
      label: 'Email theme'

symfony_mailer.email_adjuster_plugin.mailer_default_headers:
  type: mapping
  label: 'Default headers'
+2 −0
Changes for modules/symfony_mailer_bc/config/install/symfony_mailer.mailer_policy._.yml: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,3 +7,5 @@ configuration:
  mailer_url_to_absolute: {}
  mailer_html_to_text: {}
  mailer_inline_css: {}
  email_theme:
    theme: _active_fallback
+31 −1
Changes for src/Email.php: 31 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\symfony_mailer\Processor\EmailProcessorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -30,6 +31,11 @@ class Email implements InternalEmailInterface {
   */
  protected EntityTypeManagerInterface $entityTypeManager;

  /**
   * The theme manager.
   */
  protected ThemeManagerInterface $themeManager;

  protected string $type;
  protected string $subType;
  protected string $entity_id;
@@ -40,6 +46,7 @@ class Email implements InternalEmailInterface {
  protected string $langcode;
  protected array $params = [];
  protected array $variables = [];
  protected string $theme = '';
  protected array $libraries = [];

  /**
@@ -56,6 +63,8 @@ class Email implements InternalEmailInterface {
   *   The renderer.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   * @param string $type
   *   Type. @see \Drupal\symfony_mailer\BaseEmailInterface::getType()
   * @param string $sub_type
@@ -63,10 +72,11 @@ class Email implements InternalEmailInterface {
   * @param ?\Drupal\Core\Config\Entity\ConfigEntityInterface $entity
   *   Entity. @see \Drupal\symfony_mailer\BaseEmailInterface::getEntity()
   */
  public function __construct(MailerInterface $mailer, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, string $type, string $sub_type, ?ConfigEntityInterface $entity) {
  public function __construct(MailerInterface $mailer, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, string $type, string $sub_type, ?ConfigEntityInterface $entity) {
    $this->mailer = $mailer;
    $this->renderer = $renderer;
    $this->entityTypeManager = $entity_type_manager;
    $this->themeManager = $theme_manager;
    $this->type = $type;
    $this->subType = $sub_type;
    $this->entity = $entity;
@@ -95,6 +105,7 @@ class Email implements InternalEmailInterface {
      $container->get('symfony_mailer'),
      $container->get('renderer'),
      $container->get('entity_type.manager'),
      $container->get('theme.manager'),
      $type,
      $sub_type,
      $entity
@@ -267,6 +278,25 @@ class Email implements InternalEmailInterface {
    return $this->variables;
  }

  /**
   * {@inheritdoc}
   */
  public function setTheme(string $theme_name) {
    $this->valid('preBuild');
    $this->theme = $theme_name;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getTheme() {
    if (!$this->theme) {
      $this->theme = $this->themeManager->getActiveTheme()->getName();
    }
    return $this->theme;
  }

  /**
   * {@inheritdoc}
   */
+20 −0
Changes for src/EmailInterface.php: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -237,6 +237,26 @@ interface EmailInterface extends BaseEmailInterface {
   */
  public function getSuggestions(string $initial, string $join);

  /**
   * Sets the email theme.
   *
   * Valid: before building.
   *
   * @param string $theme_name
   *   The theme name to use for email.
   *
   * @return $this
   */
  public function setTheme(string $theme_name);

  /**
   * Gets the email theme name.
   *
   * @return string
   *   The email theme name.
   */
  public function getTheme();

  /**
   * Adds an asset library to use as mail CSS.
   *
+54 −7
Changes for src/Mailer.php: 54 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@ use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Theme\ThemeInitializationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\Url;
@@ -73,6 +75,20 @@ class Mailer implements MailerInterface {
   */
  protected $account;

  /**
   * The theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;

  /**
   * The theme initialization.
   *
   * @var \Drupal\Core\Theme\ThemeInitializationInterface
   */
  protected $themeInitialization;

  /**
   * Constructs the Mailer object.
   *
@@ -90,8 +106,12 @@ class Mailer implements MailerInterface {
   *   The logger channel factory.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   * @param \Drupal\Core\Theme\ThemeInitializationInterface $theme_initialization
   *   The theme initialization.
   */
  public function __construct(EventDispatcherInterface $dispatcher, RendererInterface $renderer, ModuleHandlerInterface $module_handler, LanguageDefault $language_default, LanguageManagerInterface $language_manager, LoggerChannelFactoryInterface $logger_factory, AccountInterface $account) {
  public function __construct(EventDispatcherInterface $dispatcher, RendererInterface $renderer, ModuleHandlerInterface $module_handler, LanguageDefault $language_default, LanguageManagerInterface $language_manager, LoggerChannelFactoryInterface $logger_factory, AccountInterface $account, ThemeManagerInterface $theme_manager, ThemeInitializationInterface $theme_initialization) {
    $this->dispatcher = $dispatcher;
    $this->renderer = $renderer;
    $this->moduleHandler = $module_handler;
@@ -99,6 +119,8 @@ class Mailer implements MailerInterface {
    $this->languageManager = $language_manager;
    $this->loggerFactory = $logger_factory;
    $this->account = $account;
    $this->themeManager = $theme_manager;
    $this->themeInitialization = $theme_initialization;
  }

  /**
@@ -127,15 +149,26 @@ class Mailer implements MailerInterface {
   * @internal
   */
  public function doSend(InternalEmailInterface $email) {
    // Call hooks.
    $this->invokeAll('init', $email);

    // Call hooks/processors.
    $email->process('preBuild');
    $this->invokeAll('pre_build', $email);

    $theme_name = $email->getTheme();
    $active_theme_name = $this->themeManager->getActiveTheme()->getName();
    $must_switch_theme = $theme_name !== $active_theme_name;

    if ($must_switch_theme) {
      $this->changeTheme($theme_name);
    }

    $langcode = $email->getLangcode();
    $currentLangcode = $this->languageManager->getCurrentLanguage()->getId();
    $mustSwitch = isset($langcode) && $langcode !== $currentLangcode;
    $current_langcode = $this->languageManager->getCurrentLanguage()->getId();
    $must_switch_language = isset($langcode) && $langcode !== $current_langcode;

    if ($mustSwitch) {
    if ($must_switch_language) {
      $this->changeActiveLanguage($langcode);
    }

@@ -188,8 +221,12 @@ class Mailer implements MailerInterface {
      $result = FALSE;
    }

    if ($mustSwitch) {
      $this->changeActiveLanguage($currentLangcode);
    if ($must_switch_language) {
      $this->changeActiveLanguage($current_langcode);
    }

    if ($must_switch_theme) {
      $this->changeTheme($active_theme_name);
    }

    return $result;
@@ -201,7 +238,7 @@ class Mailer implements MailerInterface {
   * @param string $langcode
   *   The langcode.
   */
  protected function changeActiveLanguage($langcode) {
  protected function changeActiveLanguage(string $langcode) {
    // Language switching adapted from commerce module.
    // @see \Drupal\commerce\MailHandler::sendMail
    if (!$this->languageManager->isMultilingual()) {
@@ -232,6 +269,16 @@ class Mailer implements MailerInterface {
    }
  }

  /**
   * Changes the active theme for email.
   *
   * @param string $theme_name
   *   The theme name.
   */
  protected function changeTheme(string $theme_name) {
    $this->themeManager->setActiveTheme($this->themeInitialization->initTheme($theme_name));
  }

  /**
   * Invoke hooks.
   *
Loading