You are here

public function ConfirmBanDeleteMultiple::submitForm in Comment IP 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 DeleteMultipleForm::submitForm

File

src/Form/ConfirmBanDeleteMultiple.php, line 58

Class

ConfirmBanDeleteMultiple
Provides the comment_ip multiple ban delete confirmation form.

Namespace

Drupal\comment_ip\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('confirm')) {
    $hostnames = [];
    foreach ($this->comments as $comment) {
      $hostnames[$comment
        ->getHostname()] = $comment
        ->getHostname();
      $comment
        ->delete();
    }
    foreach ($hostnames as $hostname) {
      $ban = \Drupal::service('ban.ip_manager');
      $ban
        ->banIp($hostname);
    }
    $count_comments = count($form_state
      ->getValue('comments'));
    $count_hosts = count($hostnames);
    $this
      ->logger('content')
      ->notice('Deleted @count_comments comments and blocked @count_hosts IPs', [
      '@count_comments' => $count_comments,
      '@count_hosts' => $count_hosts,
    ]);
    drupal_set_message($this
      ->formatPlural($count_comments, 'Deleted 1 comment and 1 IP.', 'Deleted @count comments.'));
  }
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}