Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / ban / src / Form / BanDelete.php
1 <?php
2
3 namespace Drupal\ban\Form;
4
5 use Drupal\Core\Form\ConfirmFormBase;
6 use Drupal\ban\BanIpManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12 /**
13  * Provides a form to unban IP addresses.
14  *
15  * @internal
16  */
17 class BanDelete extends ConfirmFormBase {
18
19   /**
20    * The banned IP address.
21    *
22    * @var string
23    */
24   protected $banIp;
25
26   /**
27    * The IP manager.
28    *
29    * @var \Drupal\ban\BanIpManagerInterface
30    */
31   protected $ipManager;
32
33   /**
34    * Constructs a new BanDelete object.
35    *
36    * @param \Drupal\ban\BanIpManagerInterface $ip_manager
37    *   The IP manager.
38    */
39   public function __construct(BanIpManagerInterface $ip_manager) {
40     $this->ipManager = $ip_manager;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static(
48       $container->get('ban.ip_manager')
49     );
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getFormId() {
56     return 'ban_ip_delete_form';
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getQuestion() {
63     return $this->t('Are you sure you want to unblock %ip?', ['%ip' => $this->banIp]);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getConfirmText() {
70     return $this->t('Delete');
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getCancelUrl() {
77     return new Url('ban.admin_page');
78   }
79
80   /**
81    * {@inheritdoc}
82    *
83    * @param string $ban_id
84    *   The IP address record ID to unban.
85    */
86   public function buildForm(array $form, FormStateInterface $form_state, $ban_id = '') {
87     if (!$this->banIp = $this->ipManager->findById($ban_id)) {
88       throw new NotFoundHttpException();
89     }
90     return parent::buildForm($form, $form_state);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
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());
101   }
102
103 }