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();
64 // Make sure that deleting a translation does not delete the whole entity.
65 if (!$entity->isDefaultTranslation()) {
66 $untranslated_entity = $entity->getUntranslated();
67 $untranslated_entity->removeTranslation($entity->language()->getId());
68 $untranslated_entity->save();
69 $form_state->setRedirectUrl($untranslated_entity->urlInfo('canonical'));
73 $form_state->setRedirectUrl($this->getRedirectUrl());
76 drupal_set_message($this->getDeletionMessage());
77 $this->logDeletionMessage();
83 public function getCancelUrl() {
84 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
85 $entity = $this->getEntity();
86 return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->urlInfo('canonical');
92 protected function getDeletionMessage() {
93 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
94 $entity = $this->getEntity();
96 if (!$entity->isDefaultTranslation()) {
97 return $this->t('The @entity-type %label @language translation has been deleted.', [
98 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
99 '%label' => $entity->label(),
100 '@language' => $entity->language()->getName(),
104 return $this->traitGetDeletionMessage();
110 protected function logDeletionMessage() {
111 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
112 $entity = $this->getEntity();
114 if (!$entity->isDefaultTranslation()) {
115 $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label @language translation has been deleted.', [
116 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
117 '%label' => $entity->label(),
118 '@language' => $entity->language()->getName(),
122 $this->traitLogDeletionMessage();
129 public function getQuestion() {
130 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
131 $entity = $this->getEntity();
133 if (!$entity->isDefaultTranslation()) {
134 return $this->t('Are you sure you want to delete the @language translation of the @entity-type %label?', [
135 '@language' => $entity->language()->getName(),
136 '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
137 '%label' => $this->getEntity()->label(),
141 return $this->traitGetQuestion();