You are here

class MailsystemManager in Mail System 8.4

Factory for creating mail system objects based on BasePlugin's.

Hierarchy

Expanded class hierarchy of MailsystemManager

1 file declares its use of MailsystemManager
AdminForm.php in src/Form/AdminForm.php

File

src/MailsystemManager.php, line 15

Namespace

Drupal\mailsystem
View source
class MailsystemManager extends MailManager {

  /**
   * Constants used for the configuration.
   */
  const MAILSYSTEM_TYPE_SENDING = 'sender';
  const MAILSYSTEM_TYPE_FORMATTING = 'formatter';
  const MAILSYSTEM_MODULES_CONFIG = 'modules';

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

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

  /**
   * Sets the theme manager for mailsystem.
   *
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   */
  public function setThemeManager(ThemeManagerInterface $theme_manager) {
    $this->themeManager = $theme_manager;
  }

  /**
   * Sets the theme initialization for mailsystem.
   *
   * @param \Drupal\Core\Theme\ThemeInitializationInterface $theme_initialization
   *   The theme initialization.
   */
  public function setThemeInitialization(ThemeInitializationInterface $theme_initialization) {
    $this->themeInitialization = $theme_initialization;
  }

  /**
   * {@inheritdoc}
   */
  public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {

    // Switch the theme to the configured mail theme.
    $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 {

      // 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()) {
        $this->themeManager
          ->setActiveTheme($current_active_theme);
      }
    }
    return $message;
  }

  /**
   * {@inheritdoc}
   */
  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));
  }

  /**
   * Get a Mail-Plugin instance and return it.
   *
   * @param string $module
   *   Module name which is going to send and email.
   * @param string $key
   *   (optional) The ID if the email which is being sent.
   * @param string $type
   *   (optional) A subtype, like 'sending' or 'formatting'.
   *   Use \Drupal\mailsystem\MailsystemManager\MAILSYSTEM_TYPE_SENDING and
   *   \Drupal\mailsystem\MailsystemManager\MAILSYSTEM_TYPE_FORMATTING.
   *
   * @return \Drupal\Core\Mail\MailInterface
   *   A mail plugin instance.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  protected function getPluginInstance($module, $key = '', $type = '') {
    $plugin_id = NULL;

    // List of message ids which can be configured.
    $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 there is no instance cached, try to create one.
    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];
  }

  /**
   * Retrieves the key of the theme used to render the emails.
   */
  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':

        // Fetch the theme for the current domain.
        // @todo: Reimplement this as soon as module port or similar module is around.
        if (FALSE && \Drupal::moduleHandler()
          ->moduleExists('domain_theme')) {

          // Assign the selected theme, based on the active domain.
          global $_domain;
          $domain_theme = domain_theme_lookup($_domain['domain_id']);

          // The above returns -1 on failure.
          $theme = $domain_theme != -1 ? $domain_theme['theme'] : $this->themeManager
            ->getActiveTheme()
            ->getName();
        }
        break;
    }
    return $theme;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultPluginManager::$additionalAnnotationNamespaces protected property Additional namespaces the annotation discovery mechanism should scan for annotation definitions.
DefaultPluginManager::$alterHook protected property Name of the alter hook if one should be invoked.
DefaultPluginManager::$cacheKey protected property The cache key.
DefaultPluginManager::$cacheTags protected property An array of cache tags to use for the cached definitions.
DefaultPluginManager::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. 9
DefaultPluginManager::$moduleHandler protected property The module handler to invoke the alter hook. 1
DefaultPluginManager::$namespaces protected property An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.
DefaultPluginManager::$pluginDefinitionAnnotationName protected property The name of the annotation that contains the plugin definition.
DefaultPluginManager::$pluginInterface protected property The interface each plugin should implement. 1
DefaultPluginManager::$subdir protected property The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace.
DefaultPluginManager::alterDefinitions protected function Invokes the hook to alter the definitions if the alter hook is set. 1
DefaultPluginManager::alterInfo protected function Sets the alter hook name.
DefaultPluginManager::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions 5
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::findDefinitions protected function Finds plugin definitions. 7
DefaultPluginManager::fixContextAwareDefinitions private function Fix the definitions of context-aware plugins.
DefaultPluginManager::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
DefaultPluginManager::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
DefaultPluginManager::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
DefaultPluginManager::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
DefaultPluginManager::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions 2
DefaultPluginManager::getDiscovery protected function Gets the plugin discovery. Overrides PluginManagerBase::getDiscovery 12
DefaultPluginManager::getFactory protected function Gets the plugin factory. Overrides PluginManagerBase::getFactory
DefaultPluginManager::processDefinition public function Performs extra processing on plugin definitions. 13
DefaultPluginManager::providerExists protected function Determines if the provider of a definition exists. 3
DefaultPluginManager::setCacheBackend public function Initialize the cache backend.
DefaultPluginManager::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
DefaultPluginManager::useCaches public function Disable the use of caches. Overrides CachedDiscoveryInterface::useCaches 1
DiscoveryCachedTrait::$definitions protected property Cached definitions array. 1
DiscoveryCachedTrait::getDefinition public function Overrides DiscoveryTrait::getDefinition 3
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::hasDefinition public function
MailManager::$configFactory protected property The config factory.
MailManager::$instances protected property List of already instantiated mail plugins.
MailManager::$loggerFactory protected property The logger factory.
MailManager::$renderer protected property The renderer.
MailManager::doMail public function Composes and optionally sends an email message. 1
MailManager::__construct public function Constructs the MailManager object. Overrides DefaultPluginManager::__construct
MailsystemManager::$themeInitialization protected property The theme initialization.
MailsystemManager::$themeManager protected property The theme manager.
MailsystemManager::getInstance public function Overrides PluginManagerBase::getInstance(). Overrides MailManager::getInstance
MailsystemManager::getMailTheme public function Retrieves the key of the theme used to render the emails.
MailsystemManager::getPluginInstance protected function Get a Mail-Plugin instance and return it.
MailsystemManager::mail public function Composes and optionally sends an email message. Overrides MailManager::mail
MailsystemManager::MAILSYSTEM_MODULES_CONFIG constant
MailsystemManager::MAILSYSTEM_TYPE_FORMATTING constant
MailsystemManager::MAILSYSTEM_TYPE_SENDING constant Constants used for the configuration.
MailsystemManager::setThemeInitialization public function Sets the theme initialization for mailsystem.
MailsystemManager::setThemeManager public function Sets the theme manager for mailsystem.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance 12
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.