You are here

public function FloodControlSettingsForm::buildForm in Flood control 2.0.x

Same name and namespace in other branches
  1. 1.0.x src/Form/FloodControlSettingsForm.php \Drupal\flood_control\Form\FloodControlSettingsForm::buildForm()

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/FloodControlSettingsForm.php, line 69

Class

FloodControlSettingsForm
Administration settings form.

Namespace

Drupal\flood_control\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $flood_config = $this
    ->config('user.flood');
  $flood_settings = $flood_config
    ->get();
  $options = $this
    ->getOptions();
  $counterOptions = $options['counter'];
  $timeOptions = $options['time'];

  // User module flood events.
  $form['login'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Login'),
  ];
  $form['login']['intro'] = [
    '#markup' => $this
      ->t('The website blocks login attempts when the limit within a particular time window has been reached. The website records both attempts from IP addresses and usernames. When the limit is reached the user login form cannot be used anymore. You can remove blocked usernames and IP address from the <a href=":url">Flood Unblock page</a>.', [
      ':url' => Url::fromRoute('flood_control.unblock_form')
        ->toString(),
    ]),
  ];
  $form['login']['ip_limit'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('IP login limit'),
    '#options' => array_combine($counterOptions, $counterOptions),
    '#default_value' => $flood_settings['ip_limit'],
    '#description' => $this
      ->t('The allowed number of failed login attempts from one IP address within the allowed time window.'),
  ];
  $form['login']['ip_window'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('IP time window'),
    '#options' => [
      0 => $this
        ->t('None (disabled)'),
    ] + array_map([
      $this->dateFormatter,
      'formatInterval',
    ], array_combine($timeOptions, $timeOptions)),
    '#default_value' => $flood_settings['ip_window'],
    '#description' => $this
      ->t('The allowed time window for failed IP logins.'),
  ];
  $form['login']['user_limit'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Username login limit'),
    '#options' => array_combine($counterOptions, $counterOptions),
    '#default_value' => $flood_settings['user_limit'],
    '#description' => $this
      ->t('The allowed number of failed login attempts with one username within the allowed time window.'),
  ];
  $form['login']['user_window'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Username login time window'),
    '#options' => [
      0 => $this
        ->t('None (disabled)'),
    ] + array_map([
      $this->dateFormatter,
      'formatInterval',
    ], array_combine($timeOptions, $timeOptions)),
    '#default_value' => $flood_settings['user_window'],
    '#description' => $this
      ->t('The allowed time window for failed username logins.'),
  ];

  // Contact module flood events.
  if ($this->moduleHandler
    ->moduleExists('contact')) {
    $contact_config = $this
      ->config('contact.settings');
    $contact_settings = $contact_config
      ->get();
    $form['contact'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Contact forms'),
    ];
    $form['contact']['intro'] = [
      '#markup' => $this
        ->t('The website blocks contact form submissions when the limit within a particular time window has been reached.'),
    ];
    $form['contact']['contact_threshold_limit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Sending e-mails limit'),
      '#options' => array_combine($counterOptions, $counterOptions),
      '#default_value' => $contact_settings['flood']['limit'],
      '#description' => $this
        ->t('The allowed number of submissions within the allowed time window.'),
    ];
    $form['contact']['contact_threshold_window'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Sending e-mails window'),
      '#options' => [
        0 => $this
          ->t('None (disabled)'),
      ] + array_map([
        $this->dateFormatter,
        'formatInterval',
      ], array_combine($timeOptions, $timeOptions)),
      '#default_value' => $contact_settings['flood']['interval'],
      '#description' => $this
        ->t('The allowed time window for contact form submissions.'),
    ];
  }
  return parent::buildForm($form, $form_state);
}