MessageTemplateDeleteConfirm.php in Message 8
File
src/Form/MessageTemplateDeleteConfirm.php
View source
<?php
namespace Drupal\message\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class MessageTemplateDeleteConfirm extends EntityConfirmFormBase {
public function getQuestion() {
return t('Are you sure you want to delete the message template %template?', [
'%template' => $this->entity
->label(),
]);
}
public function getConfirmText() {
return t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$number_messages = $this->entityTypeManager
->getStorage('message')
->getQuery()
->condition('template', $this->entity
->id())
->count()
->execute();
if ($number_messages) {
$caption = '<p>' . $this
->formatPlural($number_messages, '%template is used by 1 message on your site. You cannot remove this message template until you have removed all of the %template messages.', '%template is used by @count messages on your site. You may not remove %template until you have removed all of the %template messages.', [
'%template' => $this->entity
->label(),
]) . '</p>';
$form['#title'] = $this
->getQuestion();
$form['description'] = [
'#markup' => $caption,
];
return $form;
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$t_args = [
'%name' => $this->entity
->label(),
];
$this
->messenger()
->addMessage($this
->t('The message template %name has been deleted.', $t_args));
$this
->logger('content')
->notice('Deleted message template %name', $t_args);
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
public function getCancelUrl() {
return new Url('message.overview_templates');
}
}