mailcontrol.module in Mailcontrol 8
Same filename and directory in other branches
This module holds functions useful for mail control.
File
mailcontrol.moduleView source
<?php
/**
* @file
* This module holds functions useful for mail control.
*/
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function mailcontrol_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.mailcontrol':
return '<p>' . t('Have 100% control over the default mails sent by Drupal 8 out of the box.</br>
This small module extends original Drupal 8 account settings page with the ability to turn ON/OFF all standard mails.</br>
By default Drupal 8 provides no option to disable welcome emails, account cancellation confirmation or password reset.</br>
This can be useful when you don\'t want to send those emails, or you are sending them through some external services or just for development purposes, etc.</br>
You can access these settings under <a href=":url">admin/config/people/accounts</a>.</br>
The module provide only interface modifications. The underlying functionality is already there by default in Drupal 7.', [
':url' => Url::fromRoute('entity.user.admin_form')
->toString(),
]) . '</p>';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* add the enable/disable option to all standard drupal mails
*/
function mailcontrol_form_user_admin_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$config = $configs = \Drupal::configFactory()
->get('user.settings');
// Welcome (new user created by administrator)
$form['email_admin_created']['user_mail_register_admin_created_notify'] = [
'#type' => 'checkbox',
'#title' => t('Notify user when account is created by admin'),
'#default_value' => $config
->get('notify.register_admin_created'),
'#weight' => -1,
];
// Hide the settings when the cancel notify checkbox is disabled.
$form['email_admin_created']['user_mail_register_admin_created_subject']['#states'] = [
'invisible' => [
'input[name="user_mail_register_admin_created_notify"]' => [
'checked' => FALSE,
],
],
];
$form['email_admin_created']['user_mail_register_admin_created_body']['#states'] = [
'invisible' => [
'input[name="user_mail_register_admin_created_notify"]' => [
'checked' => FALSE,
],
],
];
// Welcome (awaiting approval)
$form['email_pending_approval']['user_mail_register_pending_approval_notify'] = [
'#type' => 'checkbox',
'#title' => t('Notify user when account is awaiting for approval'),
'#default_value' => $config
->get('notify.register_pending_approval'),
'#weight' => -1,
];
// Hide the settings when the cancel notify checkbox is disabled.
$form['email_pending_approval']['user_mail_register_pending_approval_subject']['#states'] = [
'invisible' => [
'input[name="user_mail_register_pending_approval_notify"]' => [
'checked' => FALSE,
],
],
];
$form['email_pending_approval']['user_mail_register_pending_approval_body']['#states'] = [
'invisible' => [
'input[name="user_mail_register_pending_approval_notify"]' => [
'checked' => FALSE,
],
],
];
// Welcome (no approval required)
$form['email_no_approval_required']['user_mail_register_no_approval_required_notify'] = [
'#type' => 'checkbox',
'#title' => t('Notify user when account is created'),
'#default_value' => $config
->get('notify.register_no_approval_required'),
'#weight' => -1,
];
// Hide the settings when the cancel notify checkbox is disabled.
$form['email_no_approval_required']['user_mail_register_no_approval_required_subject']['#states'] = [
'invisible' => [
'input[name="user_mail_register_no_approval_required_notify"]' => [
'checked' => FALSE,
],
],
];
$form['email_no_approval_required']['user_mail_register_no_approval_required_body']['#states'] = [
'invisible' => [
'input[name="user_mail_register_no_approval_required_notify"]' => [
'checked' => FALSE,
],
],
];
// Account cancellation confirmation
$form['email_cancel_confirm']['user_mail_cancel_confirm_notify'] = [
'#type' => 'checkbox',
'#title' => t('Notify user when account is canceled'),
'#default_value' => $config
->get('notify.cancel_confirm'),
'#weight' => -1,
];
// Hide the settings when the cancel notify checkbox is disabled.
$form['email_cancel_confirm']['user_mail_cancel_confirm_subject']['#states'] = [
'invisible' => [
'input[name="user_mail_cancel_confirm_notify"]' => [
'checked' => FALSE,
],
],
];
$form['email_cancel_confirm']['user_mail_cancel_confirm_body']['#states'] = [
'invisible' => [
'input[name="user_mail_cancel_confirm_notify"]' => [
'checked' => FALSE,
],
],
];
// Password recovery
$form['email_password_reset']['user_mail_password_reset_notify'] = [
'#type' => 'checkbox',
'#title' => t('Notify user for password reset'),
'#default_value' => $config
->get('notify.password_reset'),
'#weight' => -1,
];
// Hide the settings when the cancel notify checkbox is disabled.
$form['email_password_reset']['user_mail_password_reset_subject']['#states'] = [
'invisible' => [
'input[name="user_mail_password_reset_notify"]' => [
'checked' => FALSE,
],
],
];
$form['email_password_reset']['user_mail_password_reset_body']['#states'] = [
'invisible' => [
'input[name="user_mail_password_reset_notify"]' => [
'checked' => FALSE,
],
],
];
$form['#submit'][] = '_mailcontrol_form_submit';
}
function _mailcontrol_form_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
\Drupal::service('config.factory')
->getEditable('user.settings')
->set('notify.register_admin_created', $form_state
->getValue('user_mail_register_admin_created_notify'))
->set('notify.register_pending_approval', $form_state
->getValue('user_mail_register_pending_approval_notify'))
->set('notify.register_no_approval_required', $form_state
->getValue('user_mail_register_no_approval_required_notify'))
->set('notify.cancel_confirm', $form_state
->getValue('user_mail_cancel_confirm_notify'))
->set('notify.password_reset', $form_state
->getValue('user_mail_password_reset_notify'))
->save();
}
Functions
Name![]() |
Description |
---|---|
mailcontrol_form_user_admin_settings_alter | Implements hook_form_FORM_ID_alter(). |
mailcontrol_help | Implements hook_help(). |
_mailcontrol_form_submit |