You are here

public function DeleteAllCommands::allDeleteContent in Delete all 2.x

Same name and namespace in other branches
  1. 8 src/Commands/DeleteAllCommands.php \Drupal\delete_all\Commands\DeleteAllCommands::allDeleteContent()

Delete content.

@option type pick content type @usage drush delete-all-delete-content Delete content.

@command delete:all-delete-content @aliases dadc,delete-all-delete-content

Parameters

array $options: An associative array of options whose values come from cli, aliases, config, etc.

File

src/Commands/DeleteAllCommands.php, line 130

Class

DeleteAllCommands
A Drush commandfile.

Namespace

Drupal\delete_all\Commands

Code

public function allDeleteContent(array $options = [
  'type' => NULL,
]) {

  // 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 ($options['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 = $options['type'];
    if ($types != 1) {
      $content_types = $types;
      if (strpos($content_types, ',')) {
        $content_type_options = explode(',', $content_types);
      }
      else {
        $content_type_options = [
          $content_types,
        ];
      }
    }
    else {
      $content_type_options = [];
      $content_types = $this->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 = $this
        ->io()
        ->choice(dt("Choose a content type to delete. All contents of this"), $choices);

      // Return if no type is chosen.
      if ($content_type_options === 0) {
        return;
      }
      $content_type_options = [
        $content_type_options,
      ];
    }
  }
  if ($this
    ->io()
    ->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 {
    throw new UserAbortException();
  }
}