Skip to content
Snippets Groups Projects
Commit 67c1bf70 authored by Rajab Natshah's avatar Rajab Natshah
Browse files

Issue #3248644: Add a formbit on install to setup the rerouting email address...

Issue #3248644: Add a formbit on install to setup the rerouting email address configs for the Reroute Email module with default config checked for Varbase Development
parent a82cbe7b
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\EmailValidator;
/**
* Get editable config names.
......@@ -18,6 +19,10 @@ function varbase_development_get_editable_config_names() {
'system.logging' => [
'error_level' => ERROR_REPORTING_HIDE,
],
'reroute_email.settings' => [
'address' => 'dev-catchall@vardot.com',
'whitelist' => '*@vardot.com',
],
];
return $editable_cofnigs;
......@@ -46,6 +51,23 @@ function varbase_development_build_formbit(array &$formbit, FormStateInterface &
],
'#description' => t('It is recommended that sites running on production environments do not display any errors.'),
];
$formbit['address'] = [
'#type' => 'textfield',
'#title' => t('Rerouting email addresses'),
'#default_value' => 'dev-catchall@vardot.com',
'#description' => t('Provide a space, comma, or semicolon-delimited list of email addresses.<br/>Every destination email address which is not on "Whitelisted email addresses" list will be rerouted to these addresses.<br/>If the field is empty and no value is provided, all outgoing emails would be aborted and the email would be recorded in the recent log entries (if enabled).'),
'#element_validate' => ['validate_formbit_emails'],
];
$formbit['whitelist'] = [
'#type' => 'textfield',
'#title' => t('Whitelisted email addresses'),
'#default_value' => '*@vardot.com',
'#description' => ('Provide a space, comma, or semicolon-delimited list of email addresses to pass through. <br/>Every destination email address which is not on this list will be rerouted.<br/>If the field is empty and no value is provided, all outgoing emails would be rerouted.<br/>We can use wildcard email "*@example.com" to whitelist all emails by the domain.'),
'#element_validate' => ['validate_formbit_emails'],
];
}
/**
......@@ -57,7 +79,58 @@ function varbase_development_build_formbit(array &$formbit, FormStateInterface &
* Editable cofnig values.
*/
function varbase_development_submit_formbit(array $editable_config_values) {
$configFactory = \Drupal::configFactory()->getEditable('system.logging');
$configFactory->set('error_level', $editable_config_values['system.logging']['error_level']);
$configFactory->save(TRUE);
// Save the changed values for system.logging config.
$systemLoggingConfig = \Drupal::configFactory()->getEditable('system.logging');
$systemLoggingConfig->set('error_level', $editable_config_values['system.logging']['error_level']);
$systemLoggingConfig->save(TRUE);
// Save the changed values for reroute_email.settings config.
$rerouteEmailSettingsConfig = \Drupal::configFactory()->getEditable('reroute_email.settings');
$rerouteEmailSettingsConfig->set('address', $editable_config_values['reroute_email.settings']['address']);
$rerouteEmailSettingsConfig->set('whitelist', $editable_config_values['reroute_email.settings']['whitelist']);
$rerouteEmailSettingsConfig->save(TRUE);
}
/**
* Validate multiple email addresses field.
*
* @param array $element
* A field array to validate.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function validate_formbit_emails(array $element, FormStateInterface $form_state) {
// Allow only valid email addresses.
$addresses = formbit_reroute_email_split_string($form_state->getValue($element['#name']));
$email_validator = new EmailValidator();
foreach ($addresses as $address) {
if (!$email_validator->isValid($address)) {
$form_state->setErrorByName($element['#name'], t('@address is not a valid email address.', ['@address' => $address]));
}
}
// Save value in usable way to use as `to` param in drupal_mail.
// String "email@example.com; ;; , ,," save just as "email@example.com".
// This will be ignored if any validation errors occur.
$form_state->setValue($element['#name'], implode(',', $addresses));
}
/**
* Split a string into an array by pre defined allowed delimiters.
*
* Items may be separated by any number and combination of:
* spaces, commas, semicolons, or newlines.
*
* @param string $string
* A string to be split into an array.
*
* @return array
* An array of unique values from a string.
*/
function formbit_reroute_email_split_string($string) {
$array = preg_split('/[\s,;\n]+/', $string, -1, PREG_SPLIT_NO_EMPTY);
// Remove duplications.
$array = array_unique($array);
return $array;
}
......@@ -109,3 +109,14 @@ So that I can use them after the install or update.
Then I should see "Varbase update instructions"
And I should see "100%"
And I should not see "Pending updates"
@check @local @development @staging
Scenario: Check Varbase default Reroute Email settings
When I go to "/admin/config/development/reroute_email"
And I wait
Then I should see "Reroute Email"
And the "edit-enable" checkbox is checked
And I should see "dev-catchall@vardot.com" value in the "edit-address" input element
And I should see "*@vardot.com" value in the "edit-whitelist" input element
And the "edit-description" checkbox is checked
And the "edit-message" checkbox is checked
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment