You are here

public function ViewsBulkOperationsCommands::vboExecute in Views Bulk Operations (VBO) 8.3

Same name and namespace in other branches
  1. 8.2 src/Commands/ViewsBulkOperationsCommands.php \Drupal\views_bulk_operations\Commands\ViewsBulkOperationsCommands::vboExecute()
  2. 4.0.x src/Commands/ViewsBulkOperationsCommands.php \Drupal\views_bulk_operations\Commands\ViewsBulkOperationsCommands::vboExecute()

Execute an action on all results of the specified view.

Use the --verbose parameter to see progress messages.

@command views-bulk-operations:execute

@option display-id ID of the display to use. @option args View arguments (slash is a delimiter). @option exposed Exposed filters (query string format). @option batch-size Processing batch size. @option configuration Action configuration (query string format). @option user-id The ID of the user account used for performing the operation.

@usage drush views-bulk-operations:execute some_view some_action Execute some action on some view. @usage drush vbo-execute some_view some_action --args=arg1/arg2 --batch-size=50 Execute some action on some view with arg1 and arg2 as the view arguments and 50 entities processed per batch. @usage drush vbo-exec some_view some_action --configuration="key1=value1&key2=value2" Execute some action on some view with the specified action configuration.

@aliases vbo-execute, vbo-exec

Parameters

string $view_id: The ID of the view to use.

string $action_id: The ID of the action to execute.

array $options: (optional) An array of options.

Return value

string The summary message.

File

src/Commands/ViewsBulkOperationsCommands.php, line 99

Class

ViewsBulkOperationsCommands
Defines Drush commands for the module.

Namespace

Drupal\views_bulk_operations\Commands

Code

public function vboExecute($view_id, $action_id, array $options = [
  'display-id' => 'default',
  'args' => '',
  'exposed' => '',
  'batch-size' => 10,
  'configuration' => '',
  'user-id' => 1,
]) {
  if (empty($view_id) || empty($action_id)) {
    throw new \Exception($this
      ->t('You must specify the view ID and the action ID parameters.'));
  }
  $this
    ->timer($options['verbose']);

  // Prepare options.
  if ($options['args']) {
    $options['args'] = explode('/', $options['args']);
  }
  else {
    $options['args'] = [];
  }

  // Decode query string format options.
  foreach ([
    'configuration',
    'exposed',
  ] as $name) {
    if (!empty($options[$name]) && !is_array($options[$name])) {
      parse_str($options[$name], $options[$name]);
    }
    else {
      $options[$name] = [];
    }
  }
  $vbo_data = [
    'list' => [],
    'view_id' => $view_id,
    'display_id' => $options['display-id'],
    'action_id' => $action_id,
    'preconfiguration' => $options['configuration'],
    'batch' => TRUE,
    'arguments' => $options['args'],
    'exposed_input' => $options['exposed'],
    'batch_size' => $options['batch-size'],
    'relationship_id' => 'none',
    // We set the clear_on_exposed parameter to true, otherwise with empty
    // selection exposed filters are not taken into account.
    'clear_on_exposed' => TRUE,
  ];

  // Login as superuser, as drush 9 doesn't support the
  // --user parameter.
  $account = User::load($options['user-id']);
  $this->currentUser
    ->setAccount($account);

  // Initialize the view to check if parameters are correct.
  if (!($view = Views::getView($vbo_data['view_id']))) {
    throw new \Exception($this
      ->t('Incorrect view ID provided.'));
  }
  if (!$view
    ->setDisplay($vbo_data['display_id'])) {
    throw new \Exception($this
      ->t('Incorrect view display ID provided.'));
  }
  if (!empty($vbo_data['arguments'])) {
    $view
      ->setArguments($vbo_data['arguments']);
  }
  if (!empty($vbo_data['exposed_input'])) {
    $view
      ->setExposedInput($vbo_data['exposed_input']);
  }

  // We need total rows count for proper progress message display.
  $view->get_total_rows = TRUE;
  $view
    ->execute();

  // Get relationship ID if VBO field exists.
  $vbo_data['relationship_id'] = 'none';
  foreach ($view->field as $field) {
    if ($field->options['id'] === 'views_bulk_operations_bulk_form') {
      $vbo_data['relationship_id'] = $field->options['relationship'];
    }
  }

  // Get total rows count.
  $this->viewData
    ->init($view, $view
    ->getDisplay(), $vbo_data['relationship_id']);
  $vbo_data['total_results'] = $this->viewData
    ->getTotalResults($vbo_data['clear_on_exposed']);

  // Get action definition and check if action ID is correct.
  $action_definition = $this->actionManager
    ->getDefinition($action_id);
  $vbo_data['action_label'] = (string) $action_definition['label'];
  $this
    ->timer($options['verbose'], 'init');

  // Populate entity list.
  $context = [];
  do {
    $context['finished'] = 1;
    $context['message'] = '';
    ViewsBulkOperationsBatch::getList($vbo_data, $context);
    if (!empty($context['message'])) {
      $this->logger
        ->info($context['message']);
    }
  } while ($context['finished'] < 1);
  $vbo_data = $context['results'];
  $this
    ->timer($options['verbose'], 'list');

  // Execute the selected action.
  $context = [];
  do {
    $context['finished'] = 1;
    $context['message'] = '';
    ViewsBulkOperationsBatch::operation($vbo_data, $context);
    if (!empty($context['message'])) {
      $this->logger
        ->info($context['message']);
    }
  } while ($context['finished'] < 1);

  // Output a summary message.
  $operations = array_count_values($context['results']['operations']);
  $details = [];
  foreach ($operations as $op => $count) {
    $details[] = $op . ' (' . $count . ')';
  }

  // Display debug information.
  if ($options['verbose']) {
    $this
      ->timer($options['verbose'], 'execute');
    $this->logger
      ->info($this
      ->t('Initialization time: @time ms.', [
      '@time' => $this
        ->timer($options['verbose'], 'init'),
    ]));
    $this->logger
      ->info($this
      ->t('Entity list generation time: @time ms.', [
      '@time' => $this
        ->timer($options['verbose'], 'list'),
    ]));
    $this->logger
      ->info($this
      ->t('Execution time: @time ms.', [
      '@time' => $this
        ->timer($options['verbose'], 'execute'),
    ]));
  }
  return $this
    ->t('Action processing results: @results.', [
    '@results' => implode(', ', $details),
  ]);
}