EntityDeleteFormTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Url;
  6. /**
  7. * Provides a trait for an entity deletion form.
  8. *
  9. * This trait relies on the StringTranslationTrait and the logger method added
  10. * by FormBase.
  11. *
  12. * @ingroup entity_api
  13. */
  14. trait EntityDeleteFormTrait {
  15. use ConfigDependencyDeleteFormTrait;
  16. /**
  17. * Gets the entity of this form.
  18. *
  19. * Provided by \Drupal\Core\Entity\EntityForm.
  20. *
  21. * @return \Drupal\Core\Entity\EntityInterface
  22. * The entity.
  23. */
  24. abstract public function getEntity();
  25. /**
  26. * Gets the logger for a specific channel.
  27. *
  28. * Provided by \Drupal\Core\Form\FormBase.
  29. *
  30. * @param string $channel
  31. * The name of the channel.
  32. *
  33. * @return \Psr\Log\LoggerInterface
  34. * The logger for this channel.
  35. */
  36. abstract protected function logger($channel);
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getQuestion() {
  41. return $this->t('Are you sure you want to delete the @entity-type %label?', [
  42. '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
  43. '%label' => $this->getEntity()->label(),
  44. ]);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getConfirmText() {
  50. return $this->t('Delete');
  51. }
  52. /**
  53. * Gets the message to display to the user after deleting the entity.
  54. *
  55. * @return string
  56. * The translated string of the deletion message.
  57. */
  58. protected function getDeletionMessage() {
  59. $entity = $this->getEntity();
  60. return $this->t('The @entity-type %label has been deleted.', [
  61. '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
  62. '%label' => $entity->label(),
  63. ]);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getCancelUrl() {
  69. $entity = $this->getEntity();
  70. if ($entity->hasLinkTemplate('collection')) {
  71. // If available, return the collection URL.
  72. return $entity->urlInfo('collection');
  73. }
  74. else {
  75. // Otherwise fall back to the default link template.
  76. return $entity->urlInfo();
  77. }
  78. }
  79. /**
  80. * Returns the URL where the user should be redirected after deletion.
  81. *
  82. * @return \Drupal\Core\Url
  83. * The redirect URL.
  84. */
  85. protected function getRedirectUrl() {
  86. $entity = $this->getEntity();
  87. if ($entity->hasLinkTemplate('collection')) {
  88. // If available, return the collection URL.
  89. return $entity->urlInfo('collection');
  90. }
  91. else {
  92. // Otherwise fall back to the front page.
  93. return Url::fromRoute('<front>');
  94. }
  95. }
  96. /**
  97. * Logs a message about the deleted entity.
  98. */
  99. protected function logDeletionMessage() {
  100. $entity = $this->getEntity();
  101. $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label has been deleted.', [
  102. '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
  103. '%label' => $entity->label(),
  104. ]);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function submitForm(array &$form, FormStateInterface $form_state) {
  110. $this->getEntity()->delete();
  111. $this->messenger()->addStatus($this->getDeletionMessage());
  112. $form_state->setRedirectUrl($this->getCancelUrl());
  113. $this->logDeletionMessage();
  114. }
  115. }