You are here

public function AutobanBanForm::buildForm in Automatic IP ban (Autoban) 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 FormInterface::buildForm

File

src/Form/AutobanBanForm.php, line 66

Class

AutobanBanForm
Displays banned IP addresses.

Namespace

Drupal\autoban\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $rule = '') {
  if (!$rule) {
    $rules = $this->entityTypeManager
      ->getStorage('autoban')
      ->loadMultiple();
    if (empty($rules)) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('No rules for ban'), 'warning');
      return new RedirectResponse(Url::fromRoute('entity.autoban.list')
        ->toString());
    }
  }
  $form['message'] = [
    '#markup' => $rule ? $this
      ->t('The IP addresses for rule %rule will be banned.', [
      '%rule' => $rule,
    ]) : $this
      ->t('The IP addresses for all rules will be banned.'),
  ];
  $form['rule'] = [
    '#type' => 'hidden',
    '#value' => $rule,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $rule ? $this
      ->t('Ban') : $this
      ->t('Ban all'),
  ];
  $destination = $this
    ->getDestinationArray();
  $cancel_url = !empty($destination['destination']) && Url::fromRoute('<current>')
    ->toString() != $destination['destination'] ? Url::fromUserInput($destination['destination']) : Url::fromRoute('entity.autoban.list');
  $cancel_link = Link::fromTextAndUrl($this
    ->t('Cancel'), $cancel_url)
    ->toString();
  $form['actions']['cancel'] = [
    '#markup' => $cancel_link,
  ];
  return $form;
}