Skip to content

Instantly share code, notes, and snippets.

@andriyun
Last active November 22, 2022 19:56
Show Gist options
  • Save andriyun/f0902d5dcf7c8d5ffe9e1eb3f9531018 to your computer and use it in GitHub Desktop.
Save andriyun/f0902d5dcf7c8d5ffe9e1eb3f9531018 to your computer and use it in GitHub Desktop.

Revisions

  1. andriyun revised this gist Oct 11, 2018. No changes.
  2. andriyun created this gist Oct 11, 2018.
    24 changes: 24 additions & 0 deletions drupal8-change-active-theme-programmatically.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php
    /**
    * See original implementation http://cgit.drupalcode.org/mailsystem/tree/src/MailsystemManager.php#n60
    */
    // Switch the theme to the configured mail theme.
    $theme_manager = \Drupal::service('theme.manager');
    $mail_theme = '[your theme name]';
    $current_active_theme = $theme_manager->getActiveTheme();
    if ($mail_theme && $mail_theme != $current_active_theme->getName()) {
    $theme_initialization = \Drupal::service('theme.initialization');
    $theme_manager->setActiveTheme($theme_initialization->initTheme($mail_theme));
    }

    try {
    // Do your actions here.
    // .....
    }
    finally {
    // Revert the active theme, this is done inside a finally block so it is
    // executed even if an exception is thrown during sending a mail.
    if ($mail_theme != $current_active_theme->getName()) {
    $theme_manager->setActiveTheme($current_active_theme);
    }
    }