entity_generic-8.x-3.x-dev/src/Form/GenericTypeDeleteForm.php
src/Form/GenericTypeDeleteForm.php
<?php namespace Drupal\entity_generic\Form; use Drupal\Core\Entity\EntityDeleteForm; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a confirmation form for deleting an entity type. */ class GenericTypeDeleteForm extends EntityDeleteForm { /** * Entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * Creates a class instance. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * Entity type manager. */ public function __construct(EntityTypeManagerInterface $entityTypeManager) { $this->entityTypeManager = $entityTypeManager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('entity_type.manager') ); } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $num_entities = $this->entityTypeManager->getStorage($this->entity->getEntityType()->getBundleOf()) ->getQuery() ->condition('type', $this->entity->id()) ->count() ->execute(); if ($num_entities) { $caption = '<p>' . $this->formatPlural($num_entities, '%type is used by 1 object on your site. You can not remove this entity type until you have removed all of the %type entity.', '%type is used by @count objects on your site. You may not remove %type until you have removed all of the %type entities.', array('%type' => $this->entity->label())) . '</p>'; $form['#title'] = $this->getQuestion(); $form['description'] = array('#markup' => $caption); return $form; } return parent::buildForm($form, $form_state); } }