3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Form\FormStateInterface;
8 * Provides a generic base class for a content entity deletion form.
10 * @todo Re-evaluate and streamline the entity deletion form class hierarchy in
11 * https://www.drupal.org/node/2491057.
13 class ContentEntityDeleteForm extends ContentEntityConfirmFormBase {
15 use EntityDeleteFormTrait {
16 getQuestion as traitGetQuestion;
17 logDeletionMessage as traitLogDeletionMessage;
18 getDeletionMessage as traitGetDeletionMessage;
19 getCancelUrl as traitGetCancelUrl;
25 public function buildForm(array $form, FormStateInterface $form_state) {
26 $form = parent::buildForm($form, $form_state);
28 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
29 $entity = $this->getEntity();
30 if ($entity->isDefaultTranslation()) {
31 if (count($entity->getTranslationLanguages()) > 1) {
33 foreach ($entity->getTranslationLanguages() as $language) {
34 $languages[] = $language->getName();
37 $form['deleted_translations'] = [
38 '#theme' => 'item_list',
39 '#title' => $this->t('The following @entity-type translations will be deleted:', [
40 '@entity-type' => $entity->getEntityType()->getLowercaseLabel()
42 '#items' => $languages,
45 $form['actions']['submit']['#value'] = $this->t('Delete all translations');
49 $form['actions']['submit']['#value'] = $this->t('Delete @language translation', ['@language' => $entity->language()->getName()]);
58 public function submitForm(array &$form, FormStateInterface $form_state) {
59 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
60 $entity = $this->getEntity();
62 // Make sure that deleting a translation does not delete the whole entity.
63 if (!$entity->isDefaultTranslation()) {
64 $untranslated_entity = $entity->getUntranslated();
65 $untranslated_entity->removeTranslation($entity->language()->getId());
66 $untranslated_entity->save();
67 $form_state->setRedirectUrl($untranslated_entity->urlInfo('canonical'));
71 $form_state->setRedirectUrl($this->getRedirectUrl());
74 drupal_set_message($this->getDeletionMessage());
75 $this->logDeletionMessage();
81 public function getCancelUrl() {
82 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
83 $entity = $this->getEntity();
84 return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->urlInfo('canonical');
90 protected function getDeletionMessage() {
91 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
92 $entity = $this->getEntity();
94 if (!$entity->isDefaultTranslation()) {
95 return $this->t('The @entity-type %label @language translation has been deleted.', [
96 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
97 '%label' => $entity->label(),
98 '@language' => $entity->language()->getName(),
102 return $this->traitGetDeletionMessage();
108 protected function logDeletionMessage() {
109 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
110 $entity = $this->getEntity();
112 if (!$entity->isDefaultTranslation()) {
113 $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label @language translation has been deleted.', [
114 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
115 '%label' => $entity->label(),
116 '@language' => $entity->language()->getName(),
120 $this->traitLogDeletionMessage();
127 public function getQuestion() {
128 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
129 $entity = $this->getEntity();
131 if (!$entity->isDefaultTranslation()) {
132 return $this->t('Are you sure you want to delete the @language translation of the @entity-type %label?', [
133 '@language' => $entity->language()->getName(),
134 '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
135 '%label' => $this->getEntity()->label(),
139 return $this->traitGetQuestion();