You are here

public function AdvbanSearchForm::buildForm in Advanced ban 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/AdvbanSearchForm.php, line 78

Class

AdvbanSearchForm
Search banned IP addresses.

Namespace

Drupal\advban\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $params = $this
    ->getRequest()->query
    ->all();
  $ip = !empty($params['ip']) ? $params['ip'] : '';
  $form = [];
  $form['ip'] = [
    '#title' => $this
      ->t('IP address'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#size' => 48,
    '#maxlength' => 40,
    '#default_value' => $ip,
    '#description' => $this
      ->t('Enter a valid IP address.'),
  ];
  if (!empty($ip)) {
    $ip_data = $this->ipManager
      ->isBanned($ip, [
      'expiry_check' => FALSE,
      'info_output' => TRUE,
      'no_limit' => TRUE,
    ]);
    if ($ip_data['is_banned']) {
      $header = [
        $this
          ->t('Banned IP addresses'),
        $this
          ->t('Expiration time'),
        $this
          ->t('Status'),
        $this
          ->t('Operations'),
      ];
      $rows = [];
      $ip_data_iid = $ip_data['iid'];
      if (!is_array($ip_data_iid)) {
        $ip_data_iid = [
          $ip_data_iid,
        ];
      }
      foreach ($ip_data_iid as $iid) {
        $ip_full_data = $this->ipManager
          ->findById($iid);
        if ($ip_full_data) {
          if (is_array($ip_full_data)) {
            $ip_full_data = reset($ip_full_data);
          }
          $row = $this
            ->searchResult($ip_full_data);
          $rows[] = $row;
        }
      }
      $form['ban_ip_result'] = [
        '#type' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#empty' => $this
          ->t('No blocked IP addresses available.'),
        '#weight' => 120,
      ];
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('IP address @ip has not been banned', [
        '@ip' => $ip,
      ]), 'warning');
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Search'),
  ];
  return $form;
}