You are here

public function HostMultipleDeleteConfirm::buildForm in http:BL 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 ConfirmFormBase::buildForm

File

src/Form/HostMultipleDeleteConfirm.php, line 118

Class

HostMultipleDeleteConfirm
Provides a multiple host deletion confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->hostInfo = $this->tempStoreFactory
    ->get('host_multiple_delete_confirm')
    ->get(\Drupal::currentUser()
    ->id());
  if (empty($this->hostInfo)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  /** @var \Drupal\httpbl\HostInterface[] $hosts */
  $hosts = $this->storage
    ->loadMultiple(array_keys($this->hostInfo));
  $items = [];

  // Prepare a list of any matching, banned IPs, so we can include the fact
  // they are banned in the confirmation message.
  foreach ($this->hostInfo as $id => $host_ips) {
    foreach ($host_ips as $host_ip) {
      $host = $hosts[$id];
      $key = $id . ':' . $host_ip;
      $default_key = $id . ':' . $host_ip;

      // If we have any banned hosts, we theme up some extra warning about each
      // of them.
      if ($this->banManager
        ->isBanned($host_ip)) {
        $items[$default_key] = [
          'label' => [
            '#markup' => $this
              ->t('@label - <em>Will also be un-banned.</em>', [
              '@label' => $host
                ->label(),
            ]),
          ],
          'un-banned hosts' => [
            '#theme' => 'item_list',
          ],
        ];
      }
      elseif (!isset($items[$default_key])) {
        $items[$key] = $host
          ->label();
      }
    }
  }
  $form['hosts'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  $form = parent::buildForm($form, $form_state);
  return $form;
}