You are here

function mailcontrol_form_user_admin_settings_alter in Mailcontrol 8

Same name and namespace in other branches
  1. 6 mailcontrol.module \mailcontrol_form_user_admin_settings_alter()
  2. 7 mailcontrol.module \mailcontrol_form_user_admin_settings_alter()

Implements hook_form_FORM_ID_alter().

add the enable/disable option to all standard drupal mails

File

./mailcontrol.module, line 32
This module holds functions useful for mail control.

Code

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';
}