WebformEmailProvider.php in Webform 6.x
File
src/WebformEmailProvider.php
View source
<?php
namespace Drupal\webform;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Mail\MailManagerInterface;
class WebformEmailProvider implements WebformEmailProviderInterface {
protected $configFactory;
protected $moduleHandler;
protected $mailManager;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, MailManagerInterface $mail_manager) {
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->mailManager = $mail_manager;
}
public function getModules() {
return [
'mailsystem',
'smtp',
];
}
public function check() {
if ($this->configFactory
->get('system.mail')
->get('interface.default') === 'test_mail_collector') {
return $this
->uninstall();
}
$mail_modules = $this
->getModules();
foreach ($mail_modules as $module) {
if ($this
->moduleEnabled($module)) {
return $this
->uninstall();
}
}
if ($this->configFactory
->get('system.mail')
->get('interface.default') === 'php_mail') {
return $this
->install();
}
else {
return $this
->uninstall();
}
}
public function installed() {
return $this->configFactory
->get('system.mail')
->get('interface.webform') === 'webform_php_mail';
}
public function install() {
$config = $this->configFactory
->getEditable('system.mail');
$mail_plugins = $config
->get('interface');
if (!isset($mail_plugins['webform']) || $mail_plugins['webform'] !== 'webform_php_mail') {
$mail_plugins['webform'] = 'webform_php_mail';
$config
->set('interface', $mail_plugins)
->save();
}
}
public function uninstall() {
$config = $this->configFactory
->getEditable('system.mail');
$mail_plugins = $config
->get('interface');
if (isset($mail_plugins['webform'])) {
unset($mail_plugins['webform']);
$config
->set('interface', $mail_plugins)
->save();
}
}
public function getModule() {
if ($this
->installed()) {
return 'webform';
}
else {
$modules = $this
->getModules();
foreach ($modules as $module) {
if ($this
->moduleEnabled($module)) {
return $module;
}
}
}
return FALSE;
}
public function getModuleName() {
return ($module = $this
->getModule()) ? $this->moduleHandler
->getName($module) : FALSE;
}
public function moduleEnabled($module) {
if (!$this->moduleHandler
->moduleExists($module)) {
return FALSE;
}
if ($module === 'smtp' && !$this->configFactory
->get('smtp.settings')
->get('smtp_on')) {
return FALSE;
}
return TRUE;
}
public function getMailPluginId() {
$config = $this->configFactory
->get('system.mail');
return $config
->get('interface.webform') ?: $config
->get('interface.default') ?: FALSE;
}
public function getMailPluginDefinition() {
$plugin_id = $this
->getMailPluginId();
return $plugin_id && $this->mailManager
->hasDefinition($plugin_id) ? $this->mailManager
->getDefinition($plugin_id) : NULL;
}
}