You are here

public function EmailConfirmerSettingsForm::buildForm in Email confirmer 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/EmailConfirmerSettingsForm.php, line 87

Class

EmailConfirmerSettingsForm
Email confirmer settings form.

Namespace

Drupal\email_confirmer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  /** @var \Drupal\Core\Config\ImmutableConfig $config */
  $config = $this
    ->config('email_confirmer.settings');

  // Confirmation process expiration.
  $form['hash_expiration'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Response time limit'),
    '#description' => $this
      ->t('Maximum time to attend the confirmation request from its creation.'),
    '#options' => [
      '1' => $this
        ->t('1 hour'),
    ],
    '#default_value' => $config
      ->get('hash_expiration') ? round($config
      ->get('hash_expiration') / self::SECONDS_PER_HOUR) : 24,
    '#required' => TRUE,
  ];
  for ($i = 2; $i <= 48; $i++) {
    $form['hash_expiration']['#options'][$i] = $this
      ->t('@count hours', [
      '@count' => $i,
    ]);
    if ($i > 11) {

      // Two hours step from 12h.
      $i++;
      if ($i > 23) {

        // 4 hours step from 24h.
        $i += 2;
      }
    }
  }

  // Confirmations lifetime.
  $confirmation_lifetime = $config
    ->get('confirmation_lifetime');
  $form['confirmation_lifetime'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Maximum confirmation lifetime'),
    '#description' => $this
      ->t('Confirmations older than this value will be purged from database.'),
    '#options' => [
      '1' => $this
        ->t('1 week'),
    ],
    '#default_value' => $confirmation_lifetime > 0 ? round($confirmation_lifetime / self::SECONDS_PER_WEEK) : $confirmation_lifetime,
    '#required' => TRUE,
  ];
  for ($i = 2; $i <= 7; $i++) {
    $form['confirmation_lifetime']['#options'][$i] = $this
      ->t('@count weeks', [
      '@count' => $i,
    ]);
  }
  for ($i = 2; $i <= 11; $i++) {
    $form['confirmation_lifetime']['#options'][round($i * 4.33)] = $this
      ->t('@count months', [
      '@count' => $i,
    ]);
  }
  $form['confirmation_lifetime']['#options'][52] = $this
    ->t('1 year');
  $form['confirmation_lifetime']['#options'][0] = $this
    ->t('- Do not purge -');

  // Delay before re-send request.
  $form['resendrequest_delay'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Delay before re-send request'),
    '#description' => $this
      ->t('Time lapse between same confirmation request sendings.'),
    '#options' => [],
    '#default_value' => $config
      ->get('resendrequest_delay') ? round($config
      ->get('resendrequest_delay') / 60) : 15,
    '#required' => TRUE,
  ];
  for ($i = 5; $i <= 60; $i += 5) {
    $form['resendrequest_delay']['#options'][$i] = $this
      ->t('@count minutes', [
      '@count' => $i,
    ]);
  }

  // Same IP response restriction.
  $form['restrict_same_ip'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Accept email confirmation responses only from the same IP address that was requested'),
    '#default_value' => $config
      ->get('restrict_same_ip') || FALSE,
  ];

  // Confirmation request email subject & body.
  $form['confirmationrequest_mail'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Confirmation email'),
  ];
  $form['confirmationrequest_mail']['confirmationrequest_subject'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#default_value' => $config
      ->get('confirmation_request.subject'),
    '#maxlength' => 180,
  ];
  $form['confirmationrequest_mail']['confirmationrequest_body'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Body text'),
    '#default_value' => $config
      ->get('confirmation_request.body'),
    '#rows' => 6,
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['confirmationrequest_mail']['token_help'] = [
      '#title' => $this
        ->t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $form['confirmationrequest_mail']['token_help']['browser'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'email-confirmer',
      ],
    ];
  }

  // Confirmation response form messages.
  $form['confirmationresponse'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Response form'),
  ];
  $form['confirmationresponse']['confirmationresponse_skipform'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Skip response form'),
    '#description' => $this
      ->t('Performs a direct confirmation of the email address when the user opens the confirmation URL. This disables the explicit cancellation feature and may result in false confirmations if the link is opened some way else, like by antivirus or anti-SPAM software.'),
    '#default_value' => $config
      ->get('confirmation_response.skip_confirmation_form') || FALSE,
  ];
  foreach ([
    'pending',
    'expired',
    'cancelled',
    'confirmed',
  ] as $status) {
    $form['confirmationresponse']['confirmationresponse_' . $status] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Indications for @status confirmations', [
        '@status' => $status,
      ]),
      '#description' => $this
        ->t('A text displayed in the response form for @status confirmations.', [
        '@status' => $status,
      ]),
      '#default_value' => $config
        ->get('confirmation_response.questions.' . $status),
      '#maxlength' => 255,
      '#states' => [
        'invisible' => [
          ':input[name="confirmationresponse_skipform"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }

  // Confirmation response URLs.
  $form['response_url'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Confirmation pages'),
  ];
  $form['response_url']['confirmationresponse_url_confirm'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('On confirmation URL'),
    '#description' => $this
      ->t('Default Drupal path or URL to go after an email is confirmed. Default to the front page.'),
    '#default_value' => $config
      ->get('confirmation_response.url.confirm'),
  ];
  $form['response_url']['confirmationresponse_url_cancel'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('On cancellation URL'),
    '#description' => $this
      ->t('Default Drupal path or URL to go after an email is cancelled. Default to the front page.'),
    '#default_value' => $config
      ->get('confirmation_response.url.cancel'),
  ];
  $form['response_url']['confirmationresponse_url_error'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('On error URL'),
    '#description' => $this
      ->t('Default Drupal path or URL to go where a confirmation error takes pace. Default to the front page.'),
    '#default_value' => $config
      ->get('confirmation_response.url.error'),
  ];
  return $form;
}