You are here

public function AdvbanAdmin::buildForm in Advanced ban 8

Parameters

array $form: Form variable.

Drupal\Core\Form\FormStateInterface $form_state: FormState variable.

int $ban_id: (optional) ID of the ban entry.

Overrides FormInterface::buildForm

File

src/Form/AdvbanAdmin.php, line 87

Class

AdvbanAdmin
Displays banned IP addresses.

Namespace

Drupal\advban\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $ban_id = NULL) {
  $edit_mode = !empty($ban_id);
  $ban_data = NULL;
  if ($edit_mode) {
    $ban_data = $this->ipManager
      ->findById($ban_id);
    if (empty($ban_data)) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Empty ban data for ID = @id', [
        '@id' => $ban_id,
      ]), 'error');
      return $this
        ->redirect('advban.admin_page');
    }
    $ban_data = reset($ban_data);
    if (!empty($ban_data->ip_end)) {
      $ban_data->ip = long2ip($ban_data->ip);
      $ban_data->ip_end = long2ip($ban_data->ip_end);
    }
  }
  $rows = [];
  $header = [
    $this
      ->t('Banned IP addresses'),
    $this
      ->t('Expiration time'),
    $this
      ->t('Operations'),
  ];
  $result = $this->ipManager
    ->findAll();
  foreach ($result as $ip) {
    $row = [];
    $row[] = $this->ipManager
      ->formatIp($ip->ip, $ip->ip_end);
    $row[] = empty($ip->expiry_date) ? $this
      ->t('Never') : $this->dateFormatter
      ->format($ip->expiry_date);
    $links = [];
    $links['edit'] = [
      'title' => $this
        ->t('Edit'),
      'url' => Url::fromRoute('advban.admin_page', [
        'ban_id' => $ip->iid,
      ]),
    ];
    $links['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('advban.delete', [
        'ban_id' => $ip->iid,
      ]),
    ];
    $row[] = [
      'data' => [
        '#type' => 'operations',
        '#links' => $links,
      ],
    ];
    $rows[] = $row;
  }
  $form['ip'] = [
    '#title' => $this
      ->t('IP address'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#size' => 48,
    '#maxlength' => 40,
    '#default_value' => $edit_mode ? $ban_data->ip : '',
    '#description' => $this
      ->t('Enter a valid IP address.'),
  ];
  $form['ip_end'] = [
    '#title' => $this
      ->t('IP address (end of range)'),
    '#type' => 'textfield',
    '#size' => 48,
    '#maxlength' => 40,
    '#default_value' => $edit_mode ? $ban_data->ip_end : '',
    '#description' => $this
      ->t('Enter a valid IP address (optional).'),
  ];
  $expiry_durations = $this->ipManager
    ->expiryDurations();
  $default_expiry_duration = $this
    ->config('advban.settings')
    ->get('default_expiry_duration');
  $expiry_durations_index = $this->ipManager
    ->expiryDurationIndex($expiry_durations, $default_expiry_duration);
  $form['expiry_duration'] = [
    '#title' => $this
      ->t('IP ban expiry duration'),
    '#type' => 'select',
    '#options' => [
      AdvbanHelper::ADVBAN_NEVER => $this
        ->t('Never'),
    ] + $expiry_durations,
    '#default_value' => $expiry_durations_index,
    '#description' => $this
      ->t('Select expiration duration for ban.'),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $edit_mode ? $this
      ->t('Edit') : $this
      ->t('Add'),
  ];
  if ($edit_mode) {
    $destination = $this
      ->getDestinationArray();
    if (!empty($destination)) {
      $cancel_url = Url::fromUserInput($destination['destination']);
    }
    else {
      $cancel_url = Url::fromRoute('advban.admin_page');
    }
    $cancel_link = Link::fromTextAndUrl($this
      ->t('Cancel'), $cancel_url)
      ->toString();
    $form['actions']['cancel'] = [
      '#markup' => $cancel_link,
    ];
  }
  $form['form_mode'] = [
    '#type' => 'hidden',
    '#value' => $edit_mode ? 'edit' : 'add',
  ];
  $form['ban_ip_banning_table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No blocked IP addresses available.'),
    '#weight' => 120,
  ];
  return $form;
}