You are here

public function BanAdmin::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/ban/src/Form/BanAdmin.php \Drupal\ban\Form\BanAdmin::buildForm()

Parameters

array $form: A nested array form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $default_ip: (optional) IP address to be passed on to \Drupal::formBuilder()->getForm() for use as the default value of the IP address form field.

Overrides FormInterface::buildForm

File

core/modules/ban/src/Form/BanAdmin.php, line 60

Class

BanAdmin
Displays banned IP addresses.

Namespace

Drupal\ban\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $default_ip = '') {
  $rows = [];
  $header = [
    $this
      ->t('banned IP addresses'),
    $this
      ->t('Operations'),
  ];
  $result = $this->ipManager
    ->findAll();
  foreach ($result as $ip) {
    $row = [];
    $row[] = $ip->ip;
    $links = [];
    $links['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('ban.delete', [
        'ban_id' => $ip->iid,
      ]),
    ];
    $row[] = [
      'data' => [
        '#type' => 'operations',
        '#links' => $links,
      ],
    ];
    $rows[] = $row;
  }
  $form['ip'] = [
    '#title' => $this
      ->t('IP address'),
    '#type' => 'textfield',
    '#size' => 48,
    '#maxlength' => 40,
    '#default_value' => $default_ip,
    '#description' => $this
      ->t('Enter a valid IP address.'),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add'),
  ];
  $form['ban_ip_banning_table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No blocked IP addresses available.'),
    '#weight' => 120,
  ];
  return $form;
}