Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php
- 9 core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php
Namespace
Drupal\Core\Entity
File
core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php
View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
trait EntityDeleteFormTrait {
use ConfigDependencyDeleteFormTrait;
public abstract function getEntity();
protected abstract function logger($channel);
public function getQuestion() {
return $this
->t('Are you sure you want to delete the @entity-type %label?', [
'@entity-type' => $this
->getEntity()
->getEntityType()
->getSingularLabel(),
'%label' => $this
->getEntity()
->label() ?? $this
->getEntity()
->id(),
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
protected function getDeletionMessage() {
$entity = $this
->getEntity();
return $this
->t('The @entity-type %label has been deleted.', [
'@entity-type' => $entity
->getEntityType()
->getSingularLabel(),
'%label' => $entity
->label() ?? $entity
->id(),
]);
}
public function getCancelUrl() {
$entity = $this
->getEntity();
if ($entity
->hasLinkTemplate('collection')) {
return $entity
->toUrl('collection');
}
else {
return $entity
->toUrl();
}
}
protected function getRedirectUrl() {
$entity = $this
->getEntity();
if ($entity
->hasLinkTemplate('collection')) {
return $entity
->toUrl('collection');
}
else {
return Url::fromRoute('<front>');
}
}
protected function logDeletionMessage() {
$entity = $this
->getEntity();
$this
->logger($entity
->getEntityType()
->getProvider())
->info('The @entity-type %label has been deleted.', [
'@entity-type' => $entity
->getEntityType()
->getSingularLabel(),
'%label' => $entity
->label(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->getEntity()
->delete();
$this
->messenger()
->addStatus($this
->getDeletionMessage());
$form_state
->setRedirectUrl($this
->getCancelUrl());
$this
->logDeletionMessage();
}
}
Traits