You are here

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

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

Delete users.

@option role pick roles @usage drush delete-all-delete-users Delete all users.

@command delete:all-delete-users @aliases dadu,delete-all-delete-users

Parameters

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

File

src/Commands/DeleteAllCommands.php, line 62

Class

DeleteAllCommands
A Drush commandfile.

Namespace

Drupal\delete_all\Commands

Code

public function allDeleteUsers(array $options = [
  'role' => NULL,
]) {

  // Initialize $roles as FALSE to specify that all users should be deleted.
  // This will be overriden if user provides/choses a role.
  $input_roles = FALSE;
  $deleteUser = new UserDeleteController();

  // Check for presence of '--roles' in drush command.
  if ($options['role']) {

    // func_get_args collects all keywords separated by space in an array.
    // To get the roles, we join all the keywords in a string and then use
    // 'comma' to separate them.
    $types = $options['role'];
    if ($types != 1) {
      $input_roles = $types;
      if (strpos($input_roles, ',')) {
        $input_roles = explode(',', $input_roles);
      }
      else {
        $input_roles = [
          $input_roles,
        ];
      }
    }
    else {
      $choices = [];

      // Output all roles on screen and ask user to choose one.
      $roles = user_roles();
      foreach ($roles as $key => $value) {
        $choices[$key] = $value
          ->label();
      }
      $role = $this
        ->io()
        ->choice(dt("Choose a role to delete."), $choices);

      // Return if no role is chosen.
      if ($role === 0) {
        return;
      }
      $input_roles = [
        $role,
      ];
    }
  }
  if ($this
    ->io()
    ->confirm('Are you sure you want to delete the users?')) {

    // Get users to delete.
    $users_to_delete = $deleteUser
      ->getUserToDelete($input_roles);

    // Get batch array.
    $batch = $deleteUser
      ->getUserDeleteBatch($users_to_delete);

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

    // Start the batch process.
    drush_backend_batch_process();
  }
  else {
    throw new UserAbortException();
  }
}