3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
6 use Drupal\Core\Form\FormStateInterface;
10 * Provides a trait for an entity deletion form.
12 * This trait relies on the StringTranslationTrait and the logger method added
17 trait EntityDeleteFormTrait {
18 use ConfigDependencyDeleteFormTrait;
21 * Gets the entity of this form.
23 * Provided by \Drupal\Core\Entity\EntityForm.
25 * @return \Drupal\Core\Entity\EntityInterface
28 abstract public function getEntity();
31 * Gets the logger for a specific channel.
33 * Provided by \Drupal\Core\Form\FormBase.
35 * @param string $channel
36 * The name of the channel.
38 * @return \Psr\Log\LoggerInterface
39 * The logger for this channel.
41 abstract protected function logger($channel);
46 public function getQuestion() {
47 return $this->t('Are you sure you want to delete the @entity-type %label?', [
48 '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
49 '%label' => $this->getEntity()->label(),
56 public function getConfirmText() {
57 return $this->t('Delete');
61 * Gets the message to display to the user after deleting the entity.
64 * The translated string of the deletion message.
66 protected function getDeletionMessage() {
67 $entity = $this->getEntity();
68 return $this->t('The @entity-type %label has been deleted.', [
69 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
70 '%label' => $entity->label(),
77 public function getCancelUrl() {
78 $entity = $this->getEntity();
79 if ($entity->hasLinkTemplate('collection')) {
80 // If available, return the collection URL.
81 return $entity->urlInfo('collection');
84 // Otherwise fall back to the default link template.
85 return $entity->urlInfo();
90 * Returns the URL where the user should be redirected after deletion.
92 * @return \Drupal\Core\Url
95 protected function getRedirectUrl() {
96 $entity = $this->getEntity();
97 if ($entity->hasLinkTemplate('collection')) {
98 // If available, return the collection URL.
99 return $entity->urlInfo('collection');
102 // Otherwise fall back to the front page.
103 return Url::fromRoute('<front>');
108 * Logs a message about the deleted entity.
110 protected function logDeletionMessage() {
111 $entity = $this->getEntity();
112 $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label has been deleted.', [
113 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
114 '%label' => $entity->label(),
121 public function submitForm(array &$form, FormStateInterface $form_state) {
122 $this->getEntity()->delete();
123 drupal_set_message($this->getDeletionMessage());
124 $form_state->setRedirectUrl($this->getCancelUrl());
125 $this->logDeletionMessage();