View source
<?php
namespace Drupal\httpbl\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\ban\BanIpManagerInterface;
use Drupal\httpbl\Logger\HttpblLogTrapperInterface;
use Drupal\Component\Datetime\TimeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HostDeleteForm extends ContentEntityConfirmFormBase {
protected $entity_manager;
protected $banManager;
protected $logTrapper;
public function __construct(EntityRepositoryInterface $entity_repository, BanIpManagerInterface $banManager, HttpblLogTrapperInterface $logTrapper, TimeInterface $time = NULL) {
parent::__construct($entity_repository, NULL, $time);
$this->banManager = $banManager;
$this->logTrapper = $logTrapper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('ban.ip_manager'), $container
->get('httpbl.logtrapper'), $container
->get('datetime.time'));
}
public function getQuestion() {
if ($this->banManager
->isBanned($this->entity
->label())) {
return $this
->t('Are you sure you want to delete and unban evaluated host %name?', array(
'%name' => $this->entity
->label(),
));
}
else {
return $this
->t('Are you sure you want to delete evaluated host %name?', array(
'%name' => $this->entity
->label(),
));
}
}
public function getDescription() {
if ($this->banManager
->isBanned($this->entity
->label())) {
return $this
->t('This evaluated host is also banned (Drupal core Ban). If deleted it will be unbanned. This action cannot be undone.');
}
return $this
->t('This action cannot be undone.');
}
public function getCancelUrl() {
return new Url('entity.host.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->getEntity();
if ($this->banManager
->isBanned($entity->host_ip->value)) {
$this->banManager
->unbanIp($entity->host_ip->value);
$unbanned = TRUE;
}
else {
$unbanned = FALSE;
}
$entity
->delete();
$user = $this
->currentUser();
$user = $user
->getDisplayName();
if ($unbanned) {
$this->logTrapper
->trapNotice('@type deleted and unbanned: @title, by user @user. Source: @source.', array(
'@type' => $this->entity
->bundle(),
'@title' => $this->entity
->label(),
'@user' => $user,
'@source' => $this->entity
->getSource(),
'link' => $this->entity
->projectLink(),
));
drupal_set_message($this
->t('Evaluated host @ip was deleted and unbanned.', array(
'@ip' => $entity->host_ip->value,
)));
}
else {
$this->logTrapper
->trapNotice('@type deleted: @title, by user @user. Source: @source.', array(
'@type' => $this->entity
->bundle(),
'@title' => $this->entity
->label(),
'@user' => $user,
'@source' => $this->entity
->getSource(),
'link' => $this->entity
->projectLink(),
));
drupal_set_message($this
->t('Evaluated host @ip was deleted.', array(
'@ip' => $entity->host_ip->value,
)));
}
$form_state
->setRedirect('entity.host.collection');
}
}