diff -u b/src/Form/AdminForm.php b/src/Form/AdminForm.php --- b/src/Form/AdminForm.php +++ b/src/Form/AdminForm.php @@ -275,7 +275,8 @@ $config->set('theme', $form_state->getValue(['mailsystem', 'default_theme'])); } - // Create a new module configuration if a module is selected. + // Create a new module configuration or update an existing one if a module + // is selected. if ($form_state->hasValue(['custom', 'custom_module']) && ($form_state->getValue(['custom', 'custom_module']) != 'none')) { $module = $form_state->getValue(['custom', 'custom_module']); $key = $form_state->getValue(['custom', 'custom_module_key']); @@ -293,29 +294,43 @@ $config_key = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.' . $module; $config_key .= !empty($key) ? '.' . $key : '.none'; - if ($formatter != 'none') { - $config->set($config_key . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING, $formatter); + if (!empty($config->get($config_key))) { + // If the adding custom module already exists, then update it. + $settings = [ + 'formatter' => $formatter, + 'sender' => $sender, + ]; + $this->updateCustomModule($config, $config_key, $settings); + $config->save(); } - if ($sender != 'none') { - $config->set($config_key . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING, $sender); + else { + // Create the new custom module configuration. + if ($formatter != 'none') { + $config->set($config_key . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING, $formatter); + } + if ($sender != 'none') { + $config->set($config_key . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING, $sender); + } } } + // Update or remove the custom modules. if ($form_state->hasValue(['custom', 'modules']) && is_array($form_state->getValue(['custom', 'modules']))) { foreach ($form_state->getValue(['custom', 'modules'], []) as $module_key => $settings) { $mailsystem_settings = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.' . $module_key; + $formatter = (!empty($settings[MailsystemManager::MAILSYSTEM_TYPE_FORMATTING])) ? $settings[MailsystemManager::MAILSYSTEM_TYPE_FORMATTING] : 'none'; + $sender = (!empty($settings[MailsystemManager::MAILSYSTEM_TYPE_SENDING])) ? $settings[MailsystemManager::MAILSYSTEM_TYPE_SENDING] : 'none'; if (!empty($settings['remove'])) { // If some checkboxs are checked, remove these rows. $config->clear($mailsystem_settings); } - else { + elseif (($formatter != 'none') || $sender != 'none') { // Update formatter and/or sender. - if (!empty($settings[MailsystemManager::MAILSYSTEM_TYPE_FORMATTING])) { - $config->set($mailsystem_settings . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING, $settings[MailsystemManager::MAILSYSTEM_TYPE_FORMATTING]); - } - if (!empty($settings[MailsystemManager::MAILSYSTEM_TYPE_SENDING])) { - $config->set($mailsystem_settings . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING, $settings[MailsystemManager::MAILSYSTEM_TYPE_SENDING]); - } + $values = [ + 'formatter' => $formatter, + 'sender' => $sender, + ]; + $this->updateCustomModule($config, $mailsystem_settings, $values); } } } @@ -326,6 +341,27 @@ } /** + * Update the custom formatter/sending plugin value. + * + * @param string $config + * The mailsystem.settings configuration object. + * + * @param string $mailsystem_settings + * The custom module configuration entry to which we update the plugin. + * + * @param array $values + * The new custom formatter/sending plugin value to be set. + */ + protected function updateCustomModule($config, $mailsystem_settings, $values = array()) { + if (($values['formatter'] != 'none') && ($values['formatter'] != $config->get($mailsystem_settings . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING))) { + $config->set($mailsystem_settings . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING, $values['formatter']); + } + if ($values['sender'] != 'none' && ($values['sender'] != $config->get($mailsystem_settings . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING))) { + $config->set($mailsystem_settings . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING, $values['sender']); + } + } + + /** * Returns a list with all formatter plugins. * * The plugin even must implement \Drupal\Core\Mail\MailInterface or the