3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Form\FormStateInterface;
8 * Provides a generic base class for a content entity deletion form.
12 * @todo Re-evaluate and streamline the entity deletion form class hierarchy in
13 * https://www.drupal.org/node/2491057.
15 class ContentEntityDeleteForm extends ContentEntityConfirmFormBase {
17 use EntityDeleteFormTrait {
18 getQuestion as traitGetQuestion;
19 logDeletionMessage as traitLogDeletionMessage;
20 getDeletionMessage as traitGetDeletionMessage;
21 getCancelUrl as traitGetCancelUrl;
27 public function buildForm(array $form, FormStateInterface $form_state) {
28 $form = parent::buildForm($form, $form_state);
30 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
31 $entity = $this->getEntity();
32 if ($entity->isDefaultTranslation()) {
33 if (count($entity->getTranslationLanguages()) > 1) {
35 foreach ($entity->getTranslationLanguages() as $language) {
36 $languages[] = $language->getName();
39 $form['deleted_translations'] = [
40 '#theme' => 'item_list',
41 '#title' => $this->t('The following @entity-type translations will be deleted:', [
42 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
44 '#items' => $languages,
47 $form['actions']['submit']['#value'] = $this->t('Delete all translations');
51 $form['actions']['submit']['#value'] = $this->t('Delete @language translation', ['@language' => $entity->language()->getName()]);
60 public function submitForm(array &$form, FormStateInterface $form_state) {
61 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
62 $entity = $this->getEntity();
63 $message = $this->getDeletionMessage();
65 // Make sure that deleting a translation does not delete the whole entity.
66 if (!$entity->isDefaultTranslation()) {
67 $untranslated_entity = $entity->getUntranslated();
68 $untranslated_entity->removeTranslation($entity->language()->getId());
69 $untranslated_entity->save();
70 $form_state->setRedirectUrl($untranslated_entity->urlInfo('canonical'));
74 $form_state->setRedirectUrl($this->getRedirectUrl());
77 $this->messenger()->addStatus($message);
78 $this->logDeletionMessage();
84 public function getCancelUrl() {
85 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
86 $entity = $this->getEntity();
87 return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->urlInfo('canonical');
93 protected function getDeletionMessage() {
94 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
95 $entity = $this->getEntity();
97 if (!$entity->isDefaultTranslation()) {
98 return $this->t('The @entity-type %label @language translation has been deleted.', [
99 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
100 '%label' => $entity->label(),
101 '@language' => $entity->language()->getName(),
105 return $this->traitGetDeletionMessage();
111 protected function logDeletionMessage() {
112 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
113 $entity = $this->getEntity();
115 if (!$entity->isDefaultTranslation()) {
116 $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label @language translation has been deleted.', [
117 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
118 '%label' => $entity->label(),
119 '@language' => $entity->language()->getName(),
123 $this->traitLogDeletionMessage();
130 public function getQuestion() {
131 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
132 $entity = $this->getEntity();
134 if (!$entity->isDefaultTranslation()) {
135 return $this->t('Are you sure you want to delete the @language translation of the @entity-type %label?', [
136 '@language' => $entity->language()->getName(),
137 '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
138 '%label' => $this->getEntity()->label(),
142 return $this->traitGetQuestion();