View source
<?php
namespace Drupal\mailsystem\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Url;
use Drupal\mailsystem\MailsystemManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AdminForm extends ConfigFormBase {
protected $mailManager;
protected $moduleHandler;
protected $themeHandler;
public function __construct(ConfigFactoryInterface $config_factory, MailManagerInterface $mail_manager, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct($config_factory);
$this->mailManager = $mail_manager;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.mail'), $container
->get('module_handler'), $container
->get('theme_handler'));
}
public function getFormId() {
return 'mailsystem_admin_form';
}
protected function getEditableConfigNames() {
return [
'mailsystem.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('mailsystem.settings');
$arguments = [
':interface' => Url::fromUri('https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Mail!MailInterface.php/interface/MailInterface/8')
->toString(),
'@interface' => '\\Drupal\\Core\\Mail\\MailInterface',
':format' => Url::fromUri('https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Mail!MailInterface.php/function/MailInterface%3A%3Aformat/8')
->toString(),
'@format' => 'format()',
':mail' => Url::fromUri('https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Mail!MailInterface.php/function/MailInterface%3A%3Amail/8')
->toString(),
'@mail' => 'mail()',
];
$form['mailsystem'] = [
'#type' => 'details',
'#title' => $this
->t('Default Mail System'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['mailsystem']['default_formatter'] = [
'#type' => 'select',
'#title' => $this
->t('Formatter'),
'#description' => $this
->t('Select the standard plugin for formatting an email before sending it. This plugin implements <a href=":interface">@interface</a> and in special the <a href=":format">@format</a> function.', $arguments),
'#options' => $this
->getOptions(),
'#default_value' => $config
->get('defaults.formatter'),
];
$form['mailsystem']['default_sender'] = [
'#type' => 'select',
'#title' => $this
->t('Sender'),
'#description' => $this
->t('Select the standard plugin for sending an email after formatting it. This plugin implements <a href=":interface">@interface</a> and in special the <a href=":mail">@mail</a> function.', $arguments),
'#options' => $this
->getOptions(),
'#default_value' => $config
->get('defaults.sender'),
];
$form['mailsystem']['default_theme'] = [
'#type' => 'select',
'#title' => $this
->t('Theme to render the emails'),
'#description' => $this
->t('Select the theme that will be used to render emails which are configured for this. This can be either the current theme, the default theme, the domain theme or any active theme.'),
'#options' => $this
->getThemesList(),
'#default_value' => $config
->get('theme'),
];
$form['custom'] = [
'#type' => 'details',
'#title' => $this
->t('Module-specific configuration'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['custom']['custom_module'] = [
'#type' => 'select',
'#title' => $this
->t('Module'),
'#options' => $this
->getModulesList(),
'#empty_option' => $this
->t('- Select -'),
];
$form['custom']['custom_module_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Key'),
'#description' => $this
->t('The key is used to identify specific mails if the module sends more than one. Leave empty to use the configuration for all mails sent by the selected module.'),
'#default_value' => '',
];
$form['custom']['custom_formatter'] = [
'#type' => 'select',
'#title' => $this
->t('Formatter plugin'),
'#options' => $this
->getOptions(),
'#empty_option' => $this
->t('- Default -'),
];
$form['custom']['custom_sender'] = [
'#type' => 'select',
'#title' => $this
->t('Sender plugin'),
'#options' => $this
->getOptions(),
'#empty_option' => $this
->t('- Default -'),
];
$form['custom']['add'] = [
'#type' => 'submit',
'#value' => $this
->t('Add'),
'#validate' => [
'::validateAdd',
],
'#submit' => [
'::submitAdd',
],
'#button_type' => 'primary',
];
$form['custom']['modules'] = [
'#type' => 'table',
'#header' => [
'module' => $this
->t('Module'),
'key' => $this
->t('Key'),
'formatter' => $this
->t('Formatter'),
'sender' => $this
->t('Sender'),
'remove' => $this
->t('Remove'),
],
'#empty' => $this
->t('No specific configuration yet.'),
];
$modules = $config
->get(MailsystemManager::MAILSYSTEM_MODULES_CONFIG) ?: [];
foreach ($modules as $module => $module_settings) {
if (is_array($module_settings) && $this->moduleHandler
->moduleExists($module)) {
foreach ($module_settings as $key => $settings) {
$module_key = $module . '.' . $key;
$row = [
'module' => [
'#markup' => $this->moduleHandler
->getName($module),
],
'key' => [
'#markup' => $key == 'none' ? $this
->t('All') : $key,
],
];
$row['formatter'] = [
'#type' => 'select',
'#title' => $this
->t('Formatter'),
'#title_display' => 'hidden',
'#options' => $this
->getOptions(),
'#empty_option' => $this
->t('- Default -'),
'#default_value' => isset($settings['formatter']) ? $settings['formatter'] : '',
];
$row['sender'] = [
'#type' => 'select',
'#title' => $this
->t('Sender'),
'#title_display' => 'hidden',
'#options' => $this
->getOptions(),
'#empty_option' => $this
->t('- Default -'),
'#default_value' => isset($settings['sender']) ? $settings['sender'] : '',
];
$row['remove'] = [
'#type' => 'checkbox',
'#default_value' => $module_key,
];
$form['custom']['modules'][$module_key] = $row;
}
}
}
return parent::buildForm($form, $form_state);
}
public function validateAdd(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue([
'custom',
'custom_module',
]) == '') {
$form_state
->setErrorByName('custom][custom_module', $this
->t('The module is required.'));
}
$config = $this
->config('mailsystem.settings');
$config_key = $this
->getModuleKeyConfigPrefix($form_state
->getValue([
'custom',
'custom_module',
]), $form_state
->getValue([
'custom',
'custom_module_key',
]));
if ($config
->get($config_key)) {
$form_state
->setErrorByName('custom][custom_module', $this
->t('An entry for this combination exists already. Use the form below to update or remove it.'));
return;
}
if ($form_state
->getValue([
'custom',
'custom_formatter',
]) == '' && $form_state
->getValue([
'custom',
'custom_sender',
]) == '') {
$form_state
->setErrorByName('custom][custom_formatter', $this
->t('At least a formatter or sender is required.'));
}
}
public function submitAdd(array &$form, FormStateInterface $form_state) {
$module = $form_state
->getValue([
'custom',
'custom_module',
]);
$key = $form_state
->getValue([
'custom',
'custom_module_key',
]);
$formatter = $form_state
->getValue([
'custom',
'custom_formatter',
]);
$sender = $form_state
->getValue([
'custom',
'custom_sender',
]);
$prefix = $this
->getModuleKeyConfigPrefix($module, $key);
$config = $this
->config('mailsystem.settings');
if ($formatter) {
$config
->set($prefix . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING, $formatter);
}
if ($sender) {
$config
->set($prefix . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING, $sender);
}
$config
->save();
$this
->messenger()
->addMessage($this
->t('The configuration has been added.'));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('mailsystem.settings');
$config
->set('defaults.formatter', $form_state
->getValue([
'mailsystem',
'default_formatter',
]));
$config
->set('defaults.sender', $form_state
->getValue([
'mailsystem',
'default_sender',
]));
$config
->set('theme', $form_state
->getValue([
'mailsystem',
'default_theme',
]));
if ($form_state
->hasValue([
'custom',
'modules',
]) && is_array($form_state
->getValue([
'custom',
'modules',
]))) {
foreach ($form_state
->getValue([
'custom',
'modules',
], []) as $module_key => $settings) {
$prefix = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.' . $module_key;
if (!empty($settings['remove'])) {
$config
->clear($prefix);
}
else {
foreach ([
MailsystemManager::MAILSYSTEM_TYPE_FORMATTING,
MailsystemManager::MAILSYSTEM_TYPE_SENDING,
] as $type) {
if (!empty($settings[$type])) {
$config
->set($prefix . '.' . $type, $settings[$type]);
}
else {
$config
->clear($prefix . '.' . $type);
}
}
}
}
}
$config
->save();
parent::submitForm($form, $form_state);
}
protected function getOptions() {
$list = [];
foreach ($this->mailManager
->getDefinitions() as $definition) {
$list[$definition['id']] = $definition['label'];
}
return $list;
}
protected function getThemesList() {
$theme_options = [
'current' => $this
->t('Current'),
'default' => $this
->t('Default'),
];
if ($this->moduleHandler
->moduleExists('domain_theme')) {
$theme_options['domain'] = $this
->t('Domain Theme');
}
foreach ($this->themeHandler
->listInfo() as $name => $theme) {
if ($theme->status === 1) {
$theme_options[$name] = $theme->info['name'];
}
}
return $theme_options;
}
protected function getModulesList() {
$list = [];
foreach ($this->moduleHandler
->getImplementations('mail') as $module) {
$list[$module] = $this->moduleHandler
->getName($module);
}
asort($list);
return $list;
}
protected function getPluginLabel($plugin_id) {
$definition = $this->mailManager
->getDefinition($plugin_id);
return isset($definition['label']) ? $definition['label'] : $this
->t('Unknown Plugin');
}
protected function getModuleKeyConfigPrefix($module, $key) {
$module_key = $module . '.' . ($key ?: 'none');
$config_key = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.' . $module_key;
return $config_key;
}
}