You are here

function drush_delete_all_delete_content in Delete all 2.x

Same name and namespace in other branches
  1. 8 drush/delete_all.drush.inc \drush_delete_all_delete_content()

Drush callback to delete content.

File

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

Code

function drush_delete_all_delete_content() {

  // Initialize $content_type_options as FALSE to specify that all
  // content of all types should be deleted.
  // This will be overriden if user provides/choses a content type.
  $content_type_options = FALSE;
  $deleteContent = new ContentDeleteController();

  // Check for presence of '--type' in drush command.
  if (drush_get_option('type')) {

    // func_get_args() collects all keywords separated by space in an array.
    // To get the content types, we join all the keywords in a string and then
    // use 'comma' to separate them.
    $types = func_get_args();
    if ($types) {
      $content_types = implode(' ', $types);
      if (strpos($content_types, ',')) {
        $content_type_options = explode(',', $content_types);
      }
      else {
        $content_type_options = [
          $content_types,
        ];
      }
    }
    else {
      $content_type_options = [];
      $content_types = \Drupal::entityTypeManager()
        ->getStorage('node_type')
        ->loadMultiple();
      foreach ($content_types as $content_type_machine_name => $content_type) {
        $choices[$content_type_machine_name] = $content_type
          ->label();
      }
      $content_type_options = drush_choice($choices, dt("Choose a content type to delete. All contents of this"));

      // Return if no role is chosen.
      if ($content_type_options === 0 || $content_type_options === FALSE) {
        drush_user_abort();
        return;
      }
      $content_type_options = [
        $content_type_options,
      ];
    }
  }
  if (drush_confirm('Are you sure you want to delete the nodes?')) {

    // Get nodes to delete.
    $nodes_to_delete = $deleteContent
      ->getContentToDelete($content_type_options);

    // Get batch array.
    $batch = $deleteContent
      ->getContentDeleteBatch($nodes_to_delete);

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

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