You are here

public function HostMultipleGreylistConfirm::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/HostMultipleGreylistConfirm.php, line 125

Class

HostMultipleGreylistConfirm
Provides a multiple host grey-listing (and un-banning) confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Retrieve temporary storage.
  $this->hostInfo = $this->tempStoreFactory
    ->get('host_multiple_greylist_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, grey-listed IPs, so we can include the
  // fact they are already grey-listed in the confirmation message.
  foreach ($this->hostInfo as $id => $host_ips) {
    foreach ($host_ips as $host_ip) {
      $host = $hosts[$id];
      $host_status = $host
        ->getHostStatus();
      $key = $id . ':' . $host_ip;
      $default_key = $id . ':' . $host_ip;

      // If we have any already grey-listed hosts, we theme notice they will
      // be ignored.
      if ($host_status == HTTPBL_LIST_GREY) {
        $items[$default_key] = [
          'label' => [
            '#markup' => $this
              ->t('Ignoring @label - <em> already grey-listed.</em>', [
              '@label' => $host
                ->label(),
            ]),
          ],
          'ignored 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;
}