You are here

public function AutobanDeleteAllForm::submitForm in Automatic IP ban (Autoban) 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/AutobanDeleteAllForm.php, line 143

Class

AutobanDeleteAllForm
Class AutobanDeleteAllForm.

Namespace

Drupal\autoban\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Criterions make.
  $criterions = [];
  $values = $form_state
    ->getValues();
  $type = trim($values['type']);
  if (!empty($type)) {
    $criterions['type'] = $type;
  }
  $message = trim($values['message']);
  if (!empty($message)) {
    $criterions['message'] = $message;
  }
  $referer = trim($values['referer']);
  if (!empty($referer)) {
    $criterions['referer'] = $referer;
  }
  $threshold = $values['threshold'];
  if ($threshold > 0) {
    $criterions['threshold'] = $threshold;
  }
  $user_type = $values['user_type'];
  if ($user_type > 0) {
    $criterions['user_type'] = $user_type;
  }
  $rule_type = $values['rule_type'];
  if ($rule_type > 0) {
    $criterions['rule_type'] = $rule_type;
  }
  $provider = $values['provider'];
  if (!empty($provider)) {
    $criterions['provider'] = $provider;
  }
  $autoban_entity = $this->entityTypeManager
    ->getStorage('autoban');
  $ids = $autoban_entity
    ->loadByProperties($criterions);
  if (!empty($ids)) {
    $autoban_entity
      ->delete($ids);
    $this
      ->messenger()
      ->addMessage($this
      ->t('Rules deleted: @count', [
      '@count' => count($ids),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('No rules deleted'), 'warning');
  }
  $form_state
    ->setRedirect('entity.autoban.list');
}