You are here

class SettingsForm in Multiple E-mail Addresses 2.x

Form builder for the admin settings page.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
multiple_email.routing.yml in ./multiple_email.routing.yml
multiple_email.routing.yml

File

src/Form/SettingsForm.php, line 16
Contains \Drupal\multiple_email\Form\SettingsForm.

Namespace

Drupal\multiple_email\Form
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 18
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 16
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.