AdvbanDelete.php in Advanced ban 8
File
src/Form/AdvbanDelete.php
View source
<?php
namespace Drupal\advban\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\advban\AdvbanIpManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AdvbanDelete extends ConfirmFormBase {
protected $banIp;
protected $banIpEnd;
protected $ipManager;
public function __construct(AdvbanIpManagerInterface $ip_manager) {
$this->ipManager = $ip_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('advban.ip_manager'));
}
public function getFormId() {
return 'advban_ip_delete_form';
}
public function getQuestion() {
$formatIp = $this->ipManager
->formatIp($this->banIp, $this->banIpEnd);
return $this
->t('Are you sure you want to unblock %ip?', [
'%ip' => $formatIp,
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return new Url('advban.admin_page');
}
public function buildForm(array $form, FormStateInterface $form_state, $ban_id = '') {
$ip_info = $this->ipManager
->findById($ban_id);
if (empty($ip_info)) {
throw new NotFoundHttpException();
}
if (is_array($ip_info)) {
$ip_info = reset($ip_info);
}
$this->banIp = $ip_info->ip;
$this->banIpEnd = $ip_info->ip_end;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->ipManager
->unbanIp($this->banIp, $this->banIpEnd);
$formatIp = $this->ipManager
->formatIp($this->banIp, $this->banIpEnd);
$this
->logger('advanced ban')
->notice('Deleted %ip', [
'%ip' => $formatIp,
]);
$this
->messenger()
->addMessage($this
->t('The IP address(es) %ip was deleted.', [
'%ip' => $formatIp,
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}