diff --git a/core/lib/Drupal/Core/Entity/ContentEntityConfirmFormBase.php b/core/lib/Drupal/Core/Entity/ContentEntityConfirmFormBase.php index bc0bdf3a27..99ac685f4f 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityConfirmFormBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityConfirmFormBase.php @@ -2,12 +2,21 @@ namespace Drupal\Core\Entity; +@trigger_error('The ' . __NAMESPACE__ . '\ContentEntityConfirmFormBase is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Instead, use \Drupal\Core\Entity\EntityConfirmFormBase. See https://www.drupal.org/node/2491057', E_USER_DEPRECATED); + use Drupal\Core\Form\ConfirmFormHelper; use Drupal\Core\Form\ConfirmFormInterface; use Drupal\Core\Form\FormStateInterface; /** * Provides a generic base class for an entity-based confirmation form. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. + * Use \Drupal\Core\Entity\EntityConfirmFormBase. + * + * @see https://www.drupal.org/node/2491057 + * + * @internal */ abstract class ContentEntityConfirmFormBase extends ContentEntityForm implements ConfirmFormInterface { diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php b/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php index be0a653fbc..2c2ffb1dfb 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php @@ -10,14 +10,7 @@ * @todo Re-evaluate and streamline the entity deletion form class hierarchy in * https://www.drupal.org/node/2491057. */ -class ContentEntityDeleteForm extends ContentEntityConfirmFormBase { - - use EntityDeleteFormTrait { - getQuestion as traitGetQuestion; - logDeletionMessage as traitLogDeletionMessage; - getDeletionMessage as traitGetDeletionMessage; - getCancelUrl as traitGetCancelUrl; - } +class ContentEntityDeleteForm extends EntityDeleteForm { /** * {@inheritdoc} @@ -76,13 +69,40 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->logDeletionMessage(); } + /** + * {@inheritdoc} + */ + public function getQuestion() { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $this->getEntity(); + if (!$entity->isDefaultTranslation()) { + return $this->t('Are you sure you want to delete the @language translation of the @entity-type %label?', [ + '@language' => $entity->language()->getName(), + '@entity-type' => $this->getEntity()->getEntityType()->getSingularLabel(), + '%label' => $this->getEntity()->label(), + ]); + } + + return $this->t('Are you sure you want to delete the @entity-type %label?', [ + '@entity-type' => $this->getEntity()->getEntityType()->getSingularLabel(), + '%label' => $this->getEntity()->label(), + ]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + /** * {@inheritdoc} */ public function getCancelUrl() { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $this->getEntity(); - return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->toUrl('canonical'); + return $entity->isDefaultTranslation() ? $this->getCancelUrl() : $entity->toUrl('canonical'); } /** @@ -100,7 +120,7 @@ protected function getDeletionMessage() { ]); } - return $this->traitGetDeletionMessage(); + return $this->getDeletionMessage(); } /** @@ -118,26 +138,8 @@ protected function logDeletionMessage() { ]); } else { - $this->traitLogDeletionMessage(); - } - } - - /** - * {@inheritdoc} - */ - public function getQuestion() { - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = $this->getEntity(); - - if (!$entity->isDefaultTranslation()) { - return $this->t('Are you sure you want to delete the @language translation of the @entity-type %label?', [ - '@language' => $entity->language()->getName(), - '@entity-type' => $this->getEntity()->getEntityType()->getSingularLabel(), - '%label' => $this->getEntity()->label(), - ]); + $this->logDeletionMessage(); } - - return $this->traitGetQuestion(); } } diff --git a/core/lib/Drupal/Core/Entity/EntityConfirmFormBase.php b/core/lib/Drupal/Core/Entity/EntityConfirmFormBase.php index d8c39e439f..8006f45024 100644 --- a/core/lib/Drupal/Core/Entity/EntityConfirmFormBase.php +++ b/core/lib/Drupal/Core/Entity/EntityConfirmFormBase.php @@ -104,4 +104,17 @@ public function save(array $form, FormStateInterface $form_state) {} */ public function delete(array $form, FormStateInterface $form_state) {} + /** + * {@inheritdoc} + */ + public function validate(array $form, FormStateInterface $form_state) { + // It is not necessary nor possible to validate a content entity in a + // confirmation form. + if ($this->getEntity() instanceof ContentEntityInterface) { + return; + } + + parent::validateForm($form, $form_state); + } + } diff --git a/core/lib/Drupal/Core/Entity/EntityDeleteForm.php b/core/lib/Drupal/Core/Entity/EntityDeleteForm.php index 54ce0830be..e5a587e7b2 100644 --- a/core/lib/Drupal/Core/Entity/EntityDeleteForm.php +++ b/core/lib/Drupal/Core/Entity/EntityDeleteForm.php @@ -2,8 +2,10 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; /** * Provides a generic base class for an entity deletion form. @@ -12,7 +14,14 @@ */ class EntityDeleteForm extends EntityConfirmFormBase { - use EntityDeleteFormTrait; + use ConfigDependencyDeleteFormTrait; + + /** + * {@inheritdoc} + */ + public function getBaseFormId() { + return $this->entity->getEntityTypeId() . '_delete_form'; + } /** * {@inheritdoc} @@ -34,6 +43,91 @@ public function buildForm(array $form, FormStateInterface $form_state) { return $form; } + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->getEntity()->delete(); + $this->messenger()->addMessage($this->getDeletionMessage()); + $form_state->setRedirectUrl($this->getCancelUrl()); + $this->logDeletionMessage(); + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->t('Are you sure you want to delete the @entity-type %label?', [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + '%label' => $this->getEntity()->label(), + ]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * Gets the message to display to the user after deleting the entity. + * + * @return string + * The translated string of the deletion message. + */ + protected function getDeletionMessage() { + $entity = $this->getEntity(); + return $this->t('The @entity-type %label has been deleted.', [ + '@entity-type' => $entity->getEntityType()->getLowercaseLabel(), + '%label' => $entity->label(), + ]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + $entity = $this->getEntity(); + if ($entity->hasLinkTemplate('collection')) { + // If available, return the collection URL. + return $entity->toUrl('collection'); + } + else { + // Otherwise fall back to the default link template. + return $entity->toUrl(); + } + } + + /** + * Returns the URL where the user should be redirected after deletion. + * + * @return \Drupal\Core\Url + * The redirect URL. + */ + protected function getRedirectUrl() { + $entity = $this->getEntity(); + if ($entity->hasLinkTemplate('collection')) { + // If available, return the collection URL. + return $entity->toUrl('collection'); + } + else { + // Otherwise fall back to the front page. + return Url::fromRoute(''); + } + } + + /** + * Logs a message about the deleted entity. + */ + protected function logDeletionMessage() { + $entity = $this->getEntity(); + $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label has been deleted.', [ + '@entity-type' => $entity->getEntityType()->getLowercaseLabel(), + '%label' => $entity->label(), + ]); + } + /** * Gets the configuration manager. * diff --git a/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php b/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php index 087d2391ef..f9a0402c95 100644 --- a/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php +++ b/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php @@ -2,6 +2,8 @@ namespace Drupal\Core\Entity; +@trigger_error('The ' . __NAMESPACE__ . '\EntityDeleteFormTrait is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Instead, use \Drupal\Core\Entity\EntityDeleteForm. See https://www.drupal.org/node/2491057', E_USER_DEPRECATED); + use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; @@ -12,6 +14,13 @@ * This trait relies on the StringTranslationTrait and the logger method added * by FormBase. * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use the + * \Drupal\Core\Entity\EntityDeleteForm instead. + * + * @see https://www.drupal.org/node/2926275 + * + * @internal + * * @ingroup entity_api */ trait EntityDeleteFormTrait { diff --git a/core/modules/shortcut/src/Form/ShortcutDeleteForm.php b/core/modules/shortcut/src/Form/ShortcutDeleteForm.php index 0e6c32c4ff..e583de19ca 100644 --- a/core/modules/shortcut/src/Form/ShortcutDeleteForm.php +++ b/core/modules/shortcut/src/Form/ShortcutDeleteForm.php @@ -12,13 +12,6 @@ */ class ShortcutDeleteForm extends ContentEntityDeleteForm { - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'shortcut_confirm_delete'; - } - /** * {@inheritdoc} */ diff --git a/core/modules/user/src/Form/UserCancelForm.php b/core/modules/user/src/Form/UserCancelForm.php index 787339e07f..af631e655a 100644 --- a/core/modules/user/src/Form/UserCancelForm.php +++ b/core/modules/user/src/Form/UserCancelForm.php @@ -2,7 +2,7 @@ namespace Drupal\user\Form; -use Drupal\Core\Entity\ContentEntityConfirmFormBase; +use Drupal\Core\Entity\EntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; /** @@ -10,7 +10,7 @@ * * @internal */ -class UserCancelForm extends ContentEntityConfirmFormBase { +class UserCancelForm extends EntityConfirmFormBase { /** * Available account cancellation methods. diff --git a/core/modules/workspaces/src/Form/WorkspaceDeleteForm.php b/core/modules/workspaces/src/Form/WorkspaceDeleteForm.php index c121e3cb1f..0be56a31f6 100644 --- a/core/modules/workspaces/src/Form/WorkspaceDeleteForm.php +++ b/core/modules/workspaces/src/Form/WorkspaceDeleteForm.php @@ -19,11 +19,11 @@ class WorkspaceDeleteForm extends ContentEntityDeleteForm implements WorkspaceFormInterface { /** - * The workspace entity. + * The entity repository. * - * @var \Drupal\workspaces\WorkspaceInterface + * @var \Drupal\workspaces\EntityRepositoryInterface */ - protected $entity; + protected $entity_repository; /** * The workspace association service.