You are here

protected function MailsystemManager::getPluginInstance in Mail System 8.4

Get a Mail-Plugin instance and return it.

Parameters

string $module: Module name which is going to send and email.

string $key: (optional) The ID if the email which is being sent.

string $type: (optional) A subtype, like 'sending' or 'formatting'. Use \Drupal\mailsystem\MailsystemManager\MAILSYSTEM_TYPE_SENDING and \Drupal\mailsystem\MailsystemManager\MAILSYSTEM_TYPE_FORMATTING.

Return value

\Drupal\Core\Mail\MailInterface A mail plugin instance.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 call to MailsystemManager::getPluginInstance()
MailsystemManager::getInstance in src/MailsystemManager.php
Overrides PluginManagerBase::getInstance().

File

src/MailsystemManager.php, line 112

Class

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

Namespace

Drupal\mailsystem

Code

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];
}