class BanDelete

Same name and namespace in other branches
  1. 11.x core/modules/ban/src/Form/BanDelete.php \Drupal\ban\Form\BanDelete

Provides a form to unban IP addresses.

@internal

Hierarchy

Expanded class hierarchy of BanDelete

1 string reference to 'BanDelete'
ban.routing.yml in core/modules/ban/ban.routing.yml
core/modules/ban/ban.routing.yml

File

core/modules/ban/src/Form/BanDelete.php, line 17

Namespace

Drupal\ban\Form
View source
class BanDelete extends ConfirmFormBase {
  
  /**
   * The banned IP address.
   *
   * @var string
   */
  protected $banIp;
  
  /**
   * The IP manager.
   *
   * @var \Drupal\ban\BanIpManagerInterface
   */
  protected $ipManager;
  
  /**
   * Constructs a new BanDelete object.
   *
   * @param \Drupal\ban\BanIpManagerInterface $ip_manager
   *   The IP manager.
   */
  public function __construct(BanIpManagerInterface $ip_manager) {
    $this->ipManager = $ip_manager;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('ban.ip_manager'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ban_ip_delete_form';
  }
  
  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to unblock %ip?', [
      '%ip' => $this->banIp,
    ]);
  }
  
  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Delete');
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('ban.admin_page');
  }
  
  /**
   * {@inheritdoc}
   *
   * @param array $form
   *   A nested array form elements comprising the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param string $ban_id
   *   The IP address record ID to unban.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $ban_id = '') {
    if (!$this->banIp = $this->ipManager
      ->findById($ban_id)) {
      throw new NotFoundHttpException();
    }
    return parent::buildForm($form, $form_state);
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->ipManager
      ->unbanIp($this->banIp);
    $this->logger('user')
      ->notice('Deleted %ip', [
      '%ip' => $this->banIp,
    ]);
    $this->messenger()
      ->addStatus($this->t('The IP address %ip was deleted.', [
      '%ip' => $this->banIp,
    ]));
    $form_state->setRedirectUrl($this->getCancelUrl());
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.