You are here

trait EntityDeleteFormTrait in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php \Drupal\Core\Entity\EntityDeleteFormTrait
  2. 9 core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php \Drupal\Core\Entity\EntityDeleteFormTrait

Provides a trait for an entity deletion form.

This trait relies on the StringTranslationTrait and the logger method added by FormBase.

Hierarchy

Related topics

File

core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php, line 17

Namespace

Drupal\Core\Entity
View source
trait EntityDeleteFormTrait {
  use ConfigDependencyDeleteFormTrait;

  /**
   * Gets the entity of this form.
   *
   * Provided by \Drupal\Core\Entity\EntityForm.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The entity.
   */
  public abstract function getEntity();

  /**
   * Gets the logger for a specific channel.
   *
   * Provided by \Drupal\Core\Form\FormBase.
   *
   * @param string $channel
   *   The name of the channel.
   *
   * @return \Psr\Log\LoggerInterface
   *   The logger for this channel.
   */
  protected abstract function logger($channel);

  /**
   * {@inheritdoc}
   */
  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(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete');
  }

  /**
   * Gets the message to display to the user after deleting the entity.
   *
   * @return string
   *   The translated string of the deletion message.
   */
  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(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    $entity = $this
      ->getEntity();
    if ($entity
      ->hasLinkTemplate('collection')) {

      // If available, return the collection URL.
      return $entity
        ->toUrl('collection');
    }
    else {

      // Otherwise fall back to the default link template.
      return $entity
        ->toUrl();
    }
  }

  /**
   * Returns the URL where the user should be redirected after deletion.
   *
   * @return \Drupal\Core\Url
   *   The redirect URL.
   */
  protected function getRedirectUrl() {
    $entity = $this
      ->getEntity();
    if ($entity
      ->hasLinkTemplate('collection')) {

      // If available, return the collection URL.
      return $entity
        ->toUrl('collection');
    }
    else {

      // Otherwise fall back to the front page.
      return Url::fromRoute('<front>');
    }
  }

  /**
   * Logs a message about the deleted entity.
   */
  protected function logDeletionMessage() {
    $entity = $this
      ->getEntity();
    $this
      ->logger($entity
      ->getEntityType()
      ->getProvider())
      ->notice('The @entity-type %label has been deleted.', [
      '@entity-type' => $entity
        ->getEntityType()
        ->getSingularLabel(),
      '%label' => $entity
        ->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->getEntity()
      ->delete();
    $this
      ->messenger()
      ->addStatus($this
      ->getDeletionMessage());
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
    $this
      ->logDeletionMessage();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigDependencyDeleteFormTrait::addDependencyListsToForm protected function Adds form elements to list affected configuration entities.
ConfigDependencyDeleteFormTrait::t abstract protected function Translates a string to the current language or to a given language.
EntityDeleteFormTrait::getCancelUrl public function 3
EntityDeleteFormTrait::getConfirmText public function 1
EntityDeleteFormTrait::getDeletionMessage protected function Gets the message to display to the user after deleting the entity. 3
EntityDeleteFormTrait::getEntity abstract public function Gets the entity of this form.
EntityDeleteFormTrait::getQuestion public function 4
EntityDeleteFormTrait::getRedirectUrl protected function Returns the URL where the user should be redirected after deletion. 4
EntityDeleteFormTrait::logDeletionMessage protected function Logs a message about the deleted entity. 2
EntityDeleteFormTrait::logger abstract protected function Gets the logger for a specific channel.
EntityDeleteFormTrait::submitForm public function 4