3 namespace Drupal\ban\Form;
5 use Drupal\Core\Form\ConfirmFormBase;
6 use Drupal\ban\BanIpManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13 * Provides a form to unban IP addresses.
17 class BanDelete extends ConfirmFormBase {
20 * The banned IP address.
29 * @var \Drupal\ban\BanIpManagerInterface
34 * Constructs a new BanDelete object.
36 * @param \Drupal\ban\BanIpManagerInterface $ip_manager
39 public function __construct(BanIpManagerInterface $ip_manager) {
40 $this->ipManager = $ip_manager;
46 public static function create(ContainerInterface $container) {
48 $container->get('ban.ip_manager')
55 public function getFormId() {
56 return 'ban_ip_delete_form';
62 public function getQuestion() {
63 return $this->t('Are you sure you want to unblock %ip?', ['%ip' => $this->banIp]);
69 public function getConfirmText() {
70 return $this->t('Delete');
76 public function getCancelUrl() {
77 return new Url('ban.admin_page');
83 * @param string $ban_id
84 * The IP address record ID to unban.
86 public function buildForm(array $form, FormStateInterface $form_state, $ban_id = '') {
87 if (!$this->banIp = $this->ipManager->findById($ban_id)) {
88 throw new NotFoundHttpException();
90 return parent::buildForm($form, $form_state);
96 public function submitForm(array &$form, FormStateInterface $form_state) {
97 $this->ipManager->unbanIp($this->banIp);
98 $this->logger('user')->notice('Deleted %ip', ['%ip' => $this->banIp]);
99 $this->messenger()->addStatus($this->t('The IP address %ip was deleted.', ['%ip' => $this->banIp]));
100 $form_state->setRedirectUrl($this->getCancelUrl());