You are here

public static function PrepareModulesEntityUninstallForm::deleteContentEntities in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm::deleteContentEntities()

Deletes the content entities of the specified entity type.

@internal This batch callback is only meant to be used by this form.

Parameters

string $entity_type_id: The entity type ID from which data will be deleted.

array|\ArrayAccess $context: The batch context array, passed by reference.

File

core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php, line 209

Class

PrepareModulesEntityUninstallForm
Provides a form removing module content entities data before uninstallation.

Namespace

Drupal\system\Form

Code

public static function deleteContentEntities($entity_type_id, &$context) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type_id);

  // Set the entity type ID in the results array so we can access 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'] = $storage
      ->getQuery()
      ->count()
      ->execute();
  }
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($entity_type_id);
  $entity_ids = $storage
    ->getQuery()
    ->sort($entity_type
    ->getKey('id'), 'ASC')
    ->range(0, 10)
    ->execute();
  if ($entities = $storage
    ->loadMultiple($entity_ids)) {
    $storage
      ->delete($entities);
  }

  // Sometimes deletes cause secondary deletes. For example, deleting a
  // taxonomy term can cause its children to be deleted too.
  $context['sandbox']['progress'] = $context['sandbox']['max'] - $storage
    ->getQuery()
    ->count()
    ->execute();

  // Inform the batch engine that we are not finished and provide an
  // estimation of the completion level we reached.
  if (count($entity_ids) > 0 && $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;
  }
}