Newer
Older
Alex Pott
committed
<?php
namespace Drupal\Core\Entity;
Angie Byron
committed
use Drupal\Core\Form\FormStateInterface;
Alex Pott
committed
/**
* Provides a generic base class for a content entity deletion form.
Angie Byron
committed
*
* @todo Re-evaluate and streamline the entity deletion form class hierarchy in
* https://www.drupal.org/node/2491057.
Alex Pott
committed
*/
class ContentEntityDeleteForm extends ContentEntityConfirmFormBase {
Angie Byron
committed
use EntityDeleteFormTrait {
getQuestion as traitGetQuestion;
logDeletionMessage as traitLogDeletionMessage;
getDeletionMessage as traitGetDeletionMessage;
getCancelUrl as traitGetCancelUrl;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
if ($entity->isDefaultTranslation()) {
if (count($entity->getTranslationLanguages()) > 1) {
$languages = [];
foreach ($entity->getTranslationLanguages() as $language) {
$languages[] = $language->getName();
}
$form['deleted_translations'] = [
Angie Byron
committed
'#theme' => 'item_list',
'#title' => $this->t('The following @entity-type translations will be deleted:', [
'@entity-type' => $entity->getEntityType()->getSingularLabel(),
Angie Byron
committed
]),
'#items' => $languages,
];
Angie Byron
committed
$form['actions']['submit']['#value'] = $this->t('Delete all translations');
}
}
else {
$form['actions']['submit']['#value'] = $this->t('Delete @language translation', ['@language' => $entity->language()->getName()]);
Angie Byron
committed
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
Alex Pott
committed
$message = $this->getDeletionMessage();
Angie Byron
committed
// Make sure that deleting a translation does not delete the whole entity.
if (!$entity->isDefaultTranslation()) {
$untranslated_entity = $entity->getUntranslated();
$untranslated_entity->removeTranslation($entity->language()->getId());
$untranslated_entity->save();
$form_state->setRedirectUrl($untranslated_entity->toUrl('canonical'));
Angie Byron
committed
}
else {
$entity->delete();
$form_state->setRedirectUrl($this->getRedirectUrl());
}
Alex Pott
committed
$this->messenger()->addStatus($message);
Angie Byron
committed
$this->logDeletionMessage();
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->toUrl('canonical');
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
protected function getDeletionMessage() {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
if (!$entity->isDefaultTranslation()) {
return $this->t('The @entity-type %label @language translation has been deleted.', [
'@entity-type' => $entity->getEntityType()->getSingularLabel(),
Angie Byron
committed
'%label' => $entity->label(),
'@language' => $entity->language()->getName(),
]);
}
return $this->traitGetDeletionMessage();
}
/**
* {@inheritdoc}
*/
protected function logDeletionMessage() {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
if (!$entity->isDefaultTranslation()) {
$this->logger($entity->getEntityType()->getProvider())->info('The @entity-type %label @language translation has been deleted.', [
'@entity-type' => $entity->getEntityType()->getSingularLabel(),
Angie Byron
committed
'%label' => $entity->label(),
'@language' => $entity->language()->getName(),
]);
}
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?', [
Angie Byron
committed
'@language' => $entity->language()->getName(),
'@entity-type' => $this->getEntity()->getEntityType()->getSingularLabel(),
Angie Byron
committed
'%label' => $this->getEntity()->label(),
]);
Angie Byron
committed
}
return $this->traitGetQuestion();
}