toolshed-8.x-1.x-dev/src/Entity/Form/EntityDeleteConfirm.php

src/Entity/Form/EntityDeleteConfirm.php
<?php

namespace Drupal\toolshed\Entity\Form;

use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * A general delete confirmation form for content entities.
 */
class EntityDeleteConfirm extends EntityDeleteForm {

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state): EntityInterface {
    // Do nothing as we are preparing to delete this entity, there typically
    // are no changes to the value generated by the form. We use the
    // \Drupal\Core\Entity\EntityForm class to allow for consistent entity form
    // routing and generation.
    return clone $this->entity;
  }

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state): void {
    // The delete form doesn't need to rebuild the entity, and we need to avoid
    // the inherited functionality from the default EntityForm.
    //
    // @see Drupal\toolshed\Entity\Form\EntityDeleteConfirm::buildEntity().
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl(): Url {
    $entity = $this->entity;

    return $entity->hasLinkTemplate('collection')
      ? $entity->toUrl('collection')
      : Url::fromRoute('system.admin_structure');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $entity = $this->entity;
    $entityType = $entity->getEntityType();

    try {
      $entity->delete();

      $this->messenger()->addStatus($this->t('@entity_type of %label has been deleted.', [
        '@entity_type' => $entityType->getLabel() ?? '',
        '%label' => $entity->label() ?? '',
      ]));

      $this->logDeletionMessage();

      $form_state->setRedirectUrl($this->getCancelUrl());
    }
    catch (EntityStorageException $e) {
      $this->messenger()->addError($this->t('Unable to delete @entity_type %label : @message', [
        '@entity_type' => $entityType->getLabel() ?? '',
        '%label' => $entity->label() ?? '',
        '@message' => $e->getMessage(),
      ]));
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc