You are here

function drush_delete_all_delete_entities in Delete all 8

Same name and namespace in other branches
  1. 2.x drush/delete_all.drush.inc \drush_delete_all_delete_entities()

Drush callback to delete entities.

File

drush/delete_all.drush.inc, line 238
delete all Drush command

Code

function drush_delete_all_delete_entities() {

  // Get complete list of content entity types
  $entities_info = [];
  $entities_info_extended = [];
  $entity_type_options = false;
  $bundle_type_options = false;
  foreach (\Drupal::entityTypeManager()
    ->getDefinitions() as $id => $definition) {
    if (is_a($definition, 'Drupal\\Core\\Entity\\ContentEntityType')) {
      $entities_info[$id] = $definition
        ->getLabel();
      $entities_info_extended[$id] = [
        'label' => $definition
          ->getLabel(),
        'entity_key_id' => $definition
          ->getKeys()['id'],
        'entity_bundle' => $definition
          ->getKeys()['bundle'],
      ];
    }
  }
  $deleteEntity = new EntityDeleteController();

  // get variables
  $vars = func_get_args();

  // Check for presence of '--type' in drush command.
  if (drush_get_option('type')) {
    if ($vars && isset($vars[0])) {
      $entity_type_options = $vars[0];
      if (!in_array($entity_type_options, array_keys($entities_info))) {
        drush_set_error('Please select a valid entity type');
        return;
      }
    }
  }
  if (!$entity_type_options) {
    $entity_type_options = drush_choice($entities_info, dt("Choose an entity type to delete. All items of this"));

    // Return if no entity is chosen or entity invalid
    if (!in_array($entity_type_options, array_keys($entities_info))) {
      return;
    }
  }
  $bundles_info = [
    'all' => 'All',
  ];
  $bundle_definitions = \Drupal::service('entity_type.bundle.info')
    ->getAllBundleInfo($entity_type_options);
  if ($bundle_definitions) {
    foreach ($bundle_definitions as $id => $definition) {
      $bundles_info[$id] = $definition['label'];
    }

    // Check for presence of '--bundle' in drush command.
    if (drush_get_option('bundle')) {
      if ($vars && isset($vars[1])) {
        $bundle_type_options = $vars[1];
        if (!in_array($bundle_type_options, array_keys($bundles_info))) {
          drush_set_error('Please select a valid bundle type');
          return;
        }
      }
    }
    if (!$bundle_type_options) {
      $bundle_type_options = drush_choice($bundles_info, dt("Choose bundle type to delete. All items of this"));
      if (!$bundle_type_options) {
        return;
      }

      // Delete all if bundle is All
      if ($bundle_type_options == 'all') {
        $bundle_type_options = false;
      }
    }
  }
  if (drush_confirm('Are you sure you want to delete the entities?')) {

    // Get entities to delete.
    $entities_to_delete = $deleteEntity
      ->getEntitiesToDelete($entity_type_options, $bundle_type_options, $entities_info_extended);
    if ($entity_type_options == 'user') {
      $entities_to_delete = array_diff($entities_to_delete, [
        0,
        1,
      ]);
    }

    // Get batch array.
    $batch = $deleteEntity
      ->getEntitiesDeleteBatch($entities_to_delete, $entity_type_options);

    // Initialize the batch.
    batch_set($batch);

    // Start the batch process.
    drush_backend_batch_process();
  }
  else {
    drush_user_abort();
  }
}