MailingListDeleteConfirmForm.php in Mailing List 8
File
src/Form/MailingListDeleteConfirmForm.php
View source
<?php
namespace Drupal\mailing_list\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MailingListDeleteConfirmForm extends EntityConfirmFormBase {
protected $queryFactory;
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$subscription_count = $this->queryFactory
->get('mailing_list_subscription')
->condition('mailing_list', $this->entity
->id())
->count()
->execute();
if ($subscription_count) {
$caption = '<p>' . $this
->formatPlural($subscription_count, 'There is 1 subscription to the %type mailing list. You can not remove this mailing list until you have removed that subscription.', 'There are @count subscriptions to the %type mailing list. You can not remove this mailing list until you have removed all its subscriptions.', [
'%type' => $this->entity
->label(),
]) . '</p>';
$form['#title'] = $this
->getQuestion();
$form['description'] = [
'#markup' => $caption,
];
$form['link'] = [
'#type' => 'link',
'#url' => Url::fromRoute('entity.mailing_list_subscription.collection'),
'#title' => $this
->t('Manage subscriptions'),
];
return $form;
}
return parent::buildForm($form, $form_state);
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.mailing_list.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
drupal_set_message($this
->t('content @type: deleted @label.', [
'@type' => $this->entity
->bundle(),
'@label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}