diff --git a/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php b/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php index 91a0aa8..8ee95ee 100644 --- a/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php +++ b/core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php @@ -31,9 +31,8 @@ public function getFormId() { */ public function getQuestion() { $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId); - return $this->t('Are you sure you want to delete all @entity_type_plural?', [ - '@entity_type_plural' => $entity_type->getPluralLabel() - ]); + + return $this->t('Are you sure you want to delete all @entity_type_plural?', ['@entity_type_plural' => $entity_type->getPluralLabel()]); } /** @@ -43,17 +42,8 @@ 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.', - 'There are @count @entity_type_plural to remove.', - [ - '@entity_type' => $entity_type->getSingularLabel(), - '@entity_type_plural' => $entity_type->getPluralLabel(), - ] - ) - . ' ' . - $this->t('This action cannot be undone.'); + + 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()]); } /** @@ -61,9 +51,8 @@ public function getDescription() { */ public function getConfirmText() { $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId); - return $this->t('Delete all @entity_type_plural', [ - '@entity_type_plural' => $entity_type->getPluralLabel(), - ]); + + return $this->t('Delete all @entity_type_plural', ['@entity_type_plural' => $entity_type->getPluralLabel()]); } /** @@ -79,7 +68,24 @@ public function getCancelUrl() { public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) { $this->entityTypeId = $entity_type_id; - $form_state->set('entity_type', $entity_type_id); + $form['entity_type_id'] = [ + '#type' => 'value', + '#value' => $entity_type_id, + ]; + + $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'), + '#description' => $this->t('Select how many items should be deleted at once in a single batch run.'), + '#default_value' => ($count < 100) ? $count : 100, + ]; return parent::buildForm($form, $form_state); } @@ -88,46 +94,63 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $entity_type_id = $form_state->get('entity_type'); - - /* @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ - $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id); + $entity_type_id = $form_state->getValue('entity_type_id'); + $limit = $form_state->getValue('limit'); + $entity_type_plural = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getPluralLabel(); $batch = [ 'title' => t('Deleting @entity_type_plural', [ - '@entity_type_plural' => $entity_type->getPluralLabel(), + '@entity_type_plural' => $entity_type_plural, ]), 'operations' => [ [ - [__CLASS__, 'deleteContentEntities'], [$entity_type_id], + [__CLASS__, 'deleteContentEntities'], [$entity_type_id, $limit], ], ], 'finished' => [__CLASS__, 'moduleBatchFinished'], - 'progress_message' => static::t('Deleting @entity_type_plural... Completed @percentage% (@current of @total).', [ - '@entity_type_plural' => $entity_type->getPluralLabel(), - ]), + 'progress_message' => '', ]; batch_set($batch); } /** - * Deletes the content entities of the module. + * Deletes the content entities of the specified entity type. * * @param string $entity_type_id * The entity type ID from which data will be deleted. - * @param array $context + * @param int $limit + * How many items to remove in a batch run. + * @param array|\ArrayAccess $context + * The batch context array, passed by reference. */ - public static function deleteContentEntities($entity_type_id, &$context) { - $entity_type_ids = \Drupal::entityQuery($entity_type_id)->range(0, 100)->execute(); - $storage = \Drupal::entityManager()->getStorage($entity_type_id); - if ($entity_types = $storage->loadMultiple($entity_type_ids)) { + public static function deleteContentEntities($entity_type_id, $limit, &$context) { + $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id); + + // Set the entity type ID in the results array so we can acess it in the + // batch finished callback. + $context['results']['entity_type_id'] = $entity_type_id; + + if (!isset($context['sandbox']['progress'])) { + $context['sandbox']['progress'] = 0; + $context['sandbox']['max'] = \Drupal::entityQuery($entity_type_id)->count()->execute(); + } + + $entity_ids = $storage->getQuery()->range(0, $limit)->execute(); + if ($entity_types = $storage->loadMultiple($entity_ids)) { $storage->delete($entity_types); } - $context['finished'] = (int) count($entity_type_ids) < 100; + $context['sandbox']['progress'] += count($entity_ids); - /* @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ - $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id); - $context['results']['entity_type_plural'] = $entity_type->getPluralLabel(); + // Inform the batch engine that we are not finished, + // and provide an estimation of the completion level we reached. + if ($context['sandbox']['progress'] != $context['sandbox']['max']) { + $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; + $context['message'] = t('Deleting items... Completed @percentage% (@current of @total).', ['@percentage' => round(100 * $context['sandbox']['progress'] / $context['sandbox']['max']), '@current' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']]); + + } + else { + $context['finished'] = 1; + } } /** @@ -137,9 +160,9 @@ public static function deleteContentEntities($entity_type_id, &$context) { * successful data deletion message. */ public static function moduleBatchFinished($success, $results, $operations) { - drupal_set_message(t('All @entity_type_plural have been deleted.', [ - '@entity_type_plural' => $results['entity_type_plural'], - ])); + $entity_type_plural = \Drupal::entityTypeManager()->getDefinition($results['entity_type_id'])->getPluralLabel(); + drupal_set_message(t('All @entity_type_plural have been deleted.', ['@entity_type_plural' => $entity_type_plural])); + return new RedirectResponse(Url::fromRoute('system.modules_uninstall')->setAbsolute()->toString()); }