You are here

class YamlFormEmailProvider in YAML Form 8

Manages and provides HTML email support.

Hierarchy

Expanded class hierarchy of YamlFormEmailProvider

1 string reference to 'YamlFormEmailProvider'
yamlform.services.yml in ./yamlform.services.yml
yamlform.services.yml
1 service uses YamlFormEmailProvider
yamlform.email_provider in ./yamlform.services.yml
Drupal\yamlform\YamlFormEmailProvider

File

src/YamlFormEmailProvider.php, line 12

Namespace

Drupal\yamlform
View source
class YamlFormEmailProvider implements YamlFormEmailProviderInterface {

  /**
   * The configuration object factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The module handler to load includes.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Mail manager service.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * Constructs a new YamlFormEmailProvider.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration object factory.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler class to use for loading includes.
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   Mail manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, MailManagerInterface $mail_manager) {
    $this->configFactory = $config_factory;
    $this->moduleHandler = $module_handler;
    $this->mailManager = $mail_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getModules() {
    return [
      // Mail System - https://www.drupal.org/project/mailsystem
      'mailsystem',
      // SMTP Authentication Support - https://www.drupal.org/project/smtp
      'smtp',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function check() {

    // Don't override the system.mail.interface.yamlform if the default interface
    // is the 'test_mail_collector'.
    if ($this->configFactory
      ->get('system.mail')
      ->get('interface.default') == 'test_mail_collector') {
      return $this
        ->uninstall();
    }

    // Check if a contrib module is handling sending email.
    $mail_modules = $this
      ->getModules();
    foreach ($mail_modules as $module) {
      if ($this->moduleHandler
        ->moduleExists($module)) {
        return $this
          ->uninstall();
      }
    }

    // Finally, check if the default mail interface and see if it still uses the
    // php_mail. This check allow unknown contrib modules to handle sending
    // HTML emails.
    if ($this->configFactory
      ->get('system.mail')
      ->get('interface.default') == 'php_mail') {
      return $this
        ->install();
    }
    else {
      return $this
        ->uninstall();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function installed() {
    return $this->configFactory
      ->get('system.mail')
      ->get('interface.yamlform') == 'yamlform_php_mail';
  }

  /**
   * {@inheritdoc}
   */
  public function install() {
    $config = $this->configFactory
      ->getEditable('system.mail');
    $mail_plugins = $config
      ->get('interface');
    $mail_plugins['yamlform'] = 'yamlform_php_mail';
    $config
      ->set('interface', $mail_plugins)
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function uninstall() {
    $config = $this->configFactory
      ->getEditable('system.mail');
    $mail_plugins = $config
      ->get('interface');
    unset($mail_plugins['yamlform']);
    $config
      ->set('interface', $mail_plugins)
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function getModule() {
    if ($this
      ->installed()) {
      return 'yamlform';
    }
    else {
      $modules = $this
        ->getModules();
      foreach ($modules as $module) {
        if ($this->moduleHandler
          ->moduleExists($module)) {
          return $module;
        }
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getModuleName() {
    return ($module = $this
      ->getModule()) ? $this->moduleHandler
      ->getName($module) : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getMailPluginId() {
    $config = $this->configFactory
      ->get('system.mail');
    return $config
      ->get('interface.yamlform') ?: $config
      ->get('interface.default') ?: FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getMailPluginDefinition() {
    $plugin_id = $this
      ->getMailPluginId();
    return $plugin_id ? $this->mailManager
      ->getDefinition($plugin_id) : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlFormEmailProvider::$configFactory protected property The configuration object factory.
YamlFormEmailProvider::$mailManager protected property Mail manager service.
YamlFormEmailProvider::$moduleHandler protected property The module handler to load includes.
YamlFormEmailProvider::check public function Check if the YAML Form module should provide support for sending HTML emails. Overrides YamlFormEmailProviderInterface::check
YamlFormEmailProvider::getMailPluginDefinition public function Get the mail back-end plugin definition. Overrides YamlFormEmailProviderInterface::getMailPluginDefinition
YamlFormEmailProvider::getMailPluginId public function Get the mail back-end plugin id. Overrides YamlFormEmailProviderInterface::getMailPluginId
YamlFormEmailProvider::getModule public function Get the HTML email provider module machine name. Overrides YamlFormEmailProviderInterface::getModule
YamlFormEmailProvider::getModuleName public function Get the HTML email provider human readable module name. Overrides YamlFormEmailProviderInterface::getModuleName
YamlFormEmailProvider::getModules public function Get list of known contrib module that support HTML email. Overrides YamlFormEmailProviderInterface::getModules
YamlFormEmailProvider::install public function Install form's PHP mail handler which supports sending HTML emails. Overrides YamlFormEmailProviderInterface::install
YamlFormEmailProvider::installed public function Check if form email handler is installed. Overrides YamlFormEmailProviderInterface::installed
YamlFormEmailProvider::uninstall public function Uninstall form's PHP mail handler which supports sending HTML emails. Overrides YamlFormEmailProviderInterface::uninstall
YamlFormEmailProvider::__construct public function Constructs a new YamlFormEmailProvider.