diff --git a/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php b/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php index 8ee95ee..ed17c8c 100644 --- a/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php +++ b/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php @@ -2,9 +2,11 @@ namespace Drupal\system\Form; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** @@ -20,6 +22,32 @@ class PrepareModulesEntityUninstallForm extends ConfirmFormBase { protected $entityTypeId; /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * Constructs a PrepareModulesEntityUninstallForm 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 getFormId() { @@ -30,7 +58,7 @@ public function getFormId() { * {@inheritdoc} */ public function getQuestion() { - $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId); + $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId); return $this->t('Are you sure you want to delete all @entity_type_plural?', ['@entity_type_plural' => $entity_type->getPluralLabel()]); } @@ -39,18 +67,14 @@ public function getQuestion() { * {@inheritdoc} */ public function getDescription() { - $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId); - // Display how many content entities there are to remove. - $count = \Drupal::entityQuery($this->entityTypeId)->count()->execute(); - - return $this->formatPlural($count, 'There is 1 @entity_type entity to remove. This action cannot be undone.', 'There are @count @entity_type_plural to remove. This action cannot be undone.', ['@entity_type' => $entity_type->getSingularLabel(), '@entity_type_plural' => $entity_type->getPluralLabel()]); + return $this->t('This action cannot be undone.
Make a backup of your database if you want to be able to restore these items.'); } /** * {@inheritdoc} */ public function getConfirmText() { - $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId); + $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId); return $this->t('Delete all @entity_type_plural', ['@entity_type_plural' => $entity_type->getPluralLabel()]); } @@ -67,19 +91,53 @@ public function getCancelUrl() { */ public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) { $this->entityTypeId = $entity_type_id; + $form = parent::buildForm($form, $form_state); + + $storage = $this->entityTypeManager->getStorage($entity_type_id); + $count = $storage->getQuery()->count()->execute(); $form['entity_type_id'] = [ '#type' => 'value', '#value' => $entity_type_id, ]; + // Display a list of the 10 entity labels, if possible. + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + if ($entity_type->hasKey('label')) { + $recent_entity_ids = $storage->getQuery() + ->sort($entity_type->getKey('id'), 'DESC') + ->pager(10) + ->execute(); + $recent_entities = $storage->loadMultiple($recent_entity_ids); + + $labels = []; + foreach ($recent_entities as $entity) { + $labels[] = $entity->label(); + } + + if ($labels) { + $form['recent_entity_labels'] = [ + '#theme' => 'item_list', + '#items' => $labels, + ]; + $form['total'] = [ + '#markup' => $this->t('And @count more @entity_type_plural.', ['@count' => $count - count($labels), '@entity_type_plural' => $entity_type->getPluralLabel()]), + '#access' => $count > count($labels), + ]; + } + } + + $form['description']['#prefix'] = '

'; + $form['description']['#suffix'] = '

'; + $form['description']['#weight'] = 5; + $form['advanced_options'] = [ '#type' => 'details', '#title' => $this->t('Advanced options'), '#weight' => 10, '#tree' => FALSE, ]; - $count = \Drupal::entityQuery($this->entityTypeId)->count()->execute(); + $form['advanced_options']['limit'] = [ '#type' => 'number', '#title' => $this->t('Limit'), @@ -87,7 +145,11 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t '#default_value' => ($count < 100) ? $count : 100, ]; - return parent::buildForm($form, $form_state); + // Make "Cancel" the default button, "Delete" a secondary red link. + $form['actions']['cancel']['#weight'] = -1; + $form['actions']['submit']['#button_type'] = 'danger'; + + return $form; } /** @@ -97,7 +159,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $entity_type_id = $form_state->getValue('entity_type_id'); $limit = $form_state->getValue('limit'); - $entity_type_plural = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getPluralLabel(); + $entity_type_plural = $this->entityTypeManager->getDefinition($entity_type_id)->getPluralLabel(); $batch = [ 'title' => t('Deleting @entity_type_plural', [ '@entity_type_plural' => $entity_type_plural,