You are here

SettingsForm.php in Multiple E-mail Addresses 2.x

File

src/Form/SettingsForm.php
View source
<?php

/**
 * @file
 * Contains \Drupal\multiple_email\Form\SettingsForm.
 */
namespace Drupal\multiple_email\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form builder for the admin settings page.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'multiple_email_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'multiple_email.settings',
      'multiple_email.mail',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config_settings = $this
      ->config('multiple_email.settings');
    $config_mail = $this
      ->config('multiple_email.mail');
    $form['hide_field'] = array(
      '#type' => 'select',
      '#title' => t('Hide E-mail Field'),
      '#description' => t('Hides the e-mail field when editing a user'),
      '#options' => array(
        'No',
        'Yes',
      ),
      '#default_value' => $config_settings
        ->get('hide_field'),
    );
    $form['edit_emails'] = array(
      '#type' => 'select',
      '#title' => t('Allow editing of emails'),
      '#description' => t('Allows editing of e-mail addresses. It is equivalent to deleting and adding a new e-mail address, as edited emails must be re-confirmed. If enabled, e-mail addresses (excluding primary) may be edited via the multiple e-mail tab.'),
      '#options' => array(
        'No',
        'Yes',
      ),
      '#default_value' => $config_settings
        ->get('edit_emails'),
    );
    $form['confirm_attempts'] = array(
      '#type' => 'textfield',
      '#size' => 4,
      '#title' => t('Confirm Attempts'),
      '#description' => t('How many times a user enters a confirmation code before a new one is generated. If set to 0, no new codes are sent after the first one.'),
      '#default_value' => $config_settings
        ->get('confirm.attempts'),
    );
    $form['confirm_deadline'] = array(
      '#type' => 'textfield',
      '#size' => 4,
      '#title' => t('Confirm Days'),
      '#description' => t('How many days a user has to enter a confirmation code. If 0, emails pending confirmation do not expire.'),
      '#default_value' => $config_settings
        ->get('confirm.deadline'),
    );
    $vars = '!username, !site, !email, !confirm_code, !confirm_url, !confirm_deadline';
    $form['confirmation_subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Confirmation E-mail Subject'),
      '#description' => t('Customize the subject of the message to be sent when a user adds a new e-mail to their account.') . '<br/>' . t('Available variables are:') . $vars,
      '#default_value' => $config_mail
        ->get('confirmation.subject'),
    );
    $form['confirmation_body'] = array(
      '#type' => 'textarea',
      '#title' => t('Confirmation E-mail Body'),
      '#description' => t('Customize the body of the message to be sent when a user adds a new e-mail to their account.') . '<br/>' . t('Available variables are:') . $vars,
      '#default_value' => $config_mail
        ->get('confirmation.body'),
    );
    $form['expiration_subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Expire E-mail Subject'),
      '#description' => t('Customize the subject of the message to be sent when an unconfirmed e-mail address expires.') . '<br/>' . t('Available variables are:') . $vars,
      '#default_value' => $config_mail
        ->get('expiration.subject'),
    );
    $form['expiration_body'] = array(
      '#type' => 'textarea',
      '#title' => t('Expire E-mail Body'),
      '#description' => t('Customize the body of the message to be sent when an unconfirmed e-mail address expires.') . '<br/>' . t('Available variables are:') . $vars,
      '#default_value' => $config_mail
        ->get('expiration.body'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!is_numeric($form_state
      ->getValue('confirm_attempts'))) {
      $form_state
        ->setErrorByName('confirm_attempts', $this
        ->t('Confirm attempts must be an number!'));
    }
    if (!is_numeric($form_state
      ->getValue('confirm_deadline'))) {
      $form_state
        ->setErrorByName('confirm_deadline', $this
        ->t('Confirm Days must be an number!'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('multiple_email.settings')
      ->set('hide_field', (bool) $form_state
      ->getValue('hide_field'))
      ->set('edit_emails', (bool) $form_state
      ->getValue('edit_emails'))
      ->set('confirm.attempts', (int) $form_state
      ->getValue('confirm_attempts'))
      ->set('confirm.deadline', (int) $form_state
      ->getValue('confirm_deadline'))
      ->save();
    $this
      ->config('multiple_email.mail')
      ->set('confirmation.subject', $form_state
      ->getValue('confirmation_subject'))
      ->set('confirmation.body', $form_state
      ->getValue('confirmation_body'))
      ->set('expiration.subject', $form_state
      ->getValue('expiration_subject'))
      ->set('expiration.body', $form_state
      ->getValue('expiration_body'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Classes

Namesort descending Description
SettingsForm Form builder for the admin settings page.