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;
class EckEntityBundleDeleteConfirm extends EntityDeleteForm {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the entity bundle %type?', [
'%type' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('eck.entity.' . $this->entity
->getEntityTypeId() . '.list');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$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);
}
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());
}
}