976119057286b80a15765ec75020b9417a4ff24c
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityDeleteFormTrait.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Url;
8
9 /**
10  * Provides a trait for an entity deletion form.
11  *
12  * This trait relies on the StringTranslationTrait and the logger method added
13  * by FormBase.
14  *
15  * @ingroup entity_api
16  */
17 trait EntityDeleteFormTrait {
18   use ConfigDependencyDeleteFormTrait;
19
20   /**
21    * Gets the entity of this form.
22    *
23    * Provided by \Drupal\Core\Entity\EntityForm.
24    *
25    * @return \Drupal\Core\Entity\EntityInterface
26    *   The entity.
27    */
28   abstract public function getEntity();
29
30   /**
31    * Gets the logger for a specific channel.
32    *
33    * Provided by \Drupal\Core\Form\FormBase.
34    *
35    * @param string $channel
36    *   The name of the channel.
37    *
38    * @return \Psr\Log\LoggerInterface
39    *   The logger for this channel.
40    */
41   abstract protected function logger($channel);
42
43   /**
44    * {@inheritdoc}
45    */
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(),
50     ]);
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getConfirmText() {
57     return $this->t('Delete');
58   }
59
60   /**
61    * Gets the message to display to the user after deleting the entity.
62    *
63    * @return string
64    *   The translated string of the deletion message.
65    */
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(),
71     ]);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
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');
82     }
83     else {
84       // Otherwise fall back to the default link template.
85       return $entity->urlInfo();
86     }
87   }
88
89   /**
90    * Returns the URL where the user should be redirected after deletion.
91    *
92    * @return \Drupal\Core\Url
93    *   The redirect URL.
94    */
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');
100     }
101     else {
102       // Otherwise fall back to the front page.
103       return Url::fromRoute('<front>');
104     }
105   }
106
107   /**
108    * Logs a message about the deleted entity.
109    */
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(),
115     ]);
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function submitForm(array &$form, FormStateInterface $form_state) {
122     $this->getEntity()->delete();
123     $this->messenger()->addStatus($this->getDeletionMessage());
124     $form_state->setRedirectUrl($this->getCancelUrl());
125     $this->logDeletionMessage();
126   }
127
128 }