You are here

EckEntityBundleDeleteConfirm.php in Entity Construction Kit (ECK) 8

File

src/Form/EntityBundle/EckEntityBundleDeleteConfirm.php
View source
<?php

namespace Drupal\eck\Form\EntityBundle;

use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Provides a form for ECK entity bundle deletion.
 *
 * @ingroup eck
 */
class EckEntityBundleDeleteConfirm extends EntityDeleteForm {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new EckEntityBundleDeleteConfirm object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to delete the entity bundle %type?', [
      '%type' => $this->entity
        ->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('eck.entity.' . $this->entity
      ->getEntityTypeId() . '.list');
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Check if any entity of this type already exists.
    $content_number = $this->entityTypeManager
      ->getStorage($this->entity
      ->getEntityType()
      ->getBundleOf())
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition('type', $this->entity
      ->id())
      ->count()
      ->execute();
    if (!empty($content_number)) {
      $warning_message = '<p>' . $this
        ->formatPlural($content_number, '%type is used by 1 entity on your site. You can not remove this entity type until you have removed all of the %type entities.', '%type is used by @count entities on your site. You may not remove %type until you have removed all of the %type entities.', [
        '%type' => $this->entity
          ->label(),
      ]) . '</p>';
      $form['#title'] = $this
        ->getQuestion();
      $form['description'] = [
        '#markup' => $warning_message,
      ];
      return $form;
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity
      ->delete();
    $t_args = [
      '%name' => $this->entity
        ->label(),
    ];
    \Drupal::messenger()
      ->addMessage($this
      ->t('The entity bundle %name has been deleted', $t_args));
    $this
      ->logger($this->entity
      ->getEntityType()
      ->getBundleOf())
      ->notice('Delete entity type %name', $t_args);
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
EckEntityBundleDeleteConfirm Provides a form for ECK entity bundle deletion.