class ContentEntityDeleteForm
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php \Drupal\Core\Entity\ContentEntityDeleteForm
- 10 core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php \Drupal\Core\Entity\ContentEntityDeleteForm
- 8.9.x core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php \Drupal\Core\Entity\ContentEntityDeleteForm
Provides a generic base class for a content entity deletion form.
@todo Re-evaluate and streamline the entity deletion form class hierarchy in https://www.drupal.org/node/2491057.
Hierarchy
- class \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements \Drupal\Core\Entity\EntityFormInterface extends \Drupal\Core\Form\FormBase
- class \Drupal\Core\Entity\ContentEntityForm implements \Drupal\Core\Entity\ContentEntityFormInterface extends \Drupal\Core\Entity\EntityForm
- class \Drupal\Core\Entity\ContentEntityConfirmFormBase implements \Drupal\Core\Form\ConfirmFormInterface extends \Drupal\Core\Entity\ContentEntityForm
- class \Drupal\Core\Entity\ContentEntityDeleteForm uses \Drupal\Core\Entity\EntityDeleteFormTrait extends \Drupal\Core\Entity\ContentEntityConfirmFormBase
- class \Drupal\Core\Entity\ContentEntityConfirmFormBase implements \Drupal\Core\Form\ConfirmFormInterface extends \Drupal\Core\Entity\ContentEntityForm
- class \Drupal\Core\Entity\ContentEntityForm implements \Drupal\Core\Entity\ContentEntityFormInterface extends \Drupal\Core\Entity\EntityForm
- class \Drupal\Core\Entity\EntityForm implements \Drupal\Core\Entity\EntityFormInterface extends \Drupal\Core\Form\FormBase
Expanded class hierarchy of ContentEntityDeleteForm
11 files declare their use of ContentEntityDeleteForm
- BlockContentDeleteForm.php in core/
modules/ block_content/ src/ Form/ BlockContentDeleteForm.php - ContentTranslationDeleteForm.php in core/
modules/ content_translation/ src/ Form/ ContentTranslationDeleteForm.php - DeleteForm.php in core/
modules/ comment/ src/ Form/ DeleteForm.php - EntityTestDeleteForm.php in core/
modules/ system/ tests/ modules/ entity_test/ src/ EntityTestDeleteForm.php - FeedDeleteForm.php in core/
modules/ aggregator/ src/ Form/ FeedDeleteForm.php
File
-
core/
lib/ Drupal/ Core/ Entity/ ContentEntityDeleteForm.php, line 13
Namespace
Drupal\Core\EntityView source
class ContentEntityDeleteForm extends ContentEntityConfirmFormBase {
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'] = [
'#theme' => 'item_list',
'#title' => $this->t('The following @entity-type translations will be deleted:', [
'@entity-type' => $entity->getEntityType()
->getSingularLabel(),
]),
'#items' => $languages,
];
$form['actions']['submit']['#value'] = $this->t('Delete all translations');
}
}
else {
$form['actions']['submit']['#value'] = $this->t('Delete @language translation', [
'@language' => $entity->language()
->getName(),
]);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
$message = $this->getDeletionMessage();
// 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'));
}
else {
$entity->delete();
$form_state->setRedirectUrl($this->getRedirectUrl());
}
$this->messenger()
->addStatus($message);
$this->logDeletionMessage();
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->toUrl('canonical');
}
/**
* {@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(),
'%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())
->notice('The @entity-type %label @language translation has been deleted.', [
'@entity-type' => $entity->getEntityType()
->getSingularLabel(),
'%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?', [
'@language' => $entity->language()
->getName(),
'@entity-type' => $this->getEntity()
->getEntityType()
->getSingularLabel(),
'%label' => $this->getEntity()
->label(),
]);
}
return $this->traitGetQuestion();
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.