BanDelete.php in Drupal 8
File
core/modules/ban/src/Form/BanDelete.php
View source
<?php
namespace Drupal\ban\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\ban\BanIpManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BanDelete extends ConfirmFormBase {
protected $banIp;
protected $ipManager;
public function __construct(BanIpManagerInterface $ip_manager) {
$this->ipManager = $ip_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('ban.ip_manager'));
}
public function getFormId() {
return 'ban_ip_delete_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to unblock %ip?', [
'%ip' => $this->banIp,
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return new Url('ban.admin_page');
}
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);
}
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());
}
}
Classes
Name |
Description |
BanDelete |
Provides a form to unban IP addresses. |