You are here

protected static function DataExport::buildBatch in Views data export 8

Builds batch export response.

Parameters

\Drupal\views\ViewExecutable $view: The view to export.

array $args: Arguments for the $view.

Return value

null|\Symfony\Component\HttpFoundation\RedirectResponse Redirect to the batching page.

1 call to DataExport::buildBatch()
DataExport::buildResponse in src/Plugin/views/display/DataExport.php
Builds up a response with the rendered view as content.

File

src/Plugin/views/display/DataExport.php, line 69

Class

DataExport
Provides a data export display plugin.

Namespace

Drupal\views_data_export\Plugin\views\display

Code

protected static function buildBatch(ViewExecutable &$view, array $args) {

  // Get total number of items.
  $view->get_total_rows = TRUE;
  $export_limit = $view
    ->getDisplay()
    ->getOption('export_limit');
  $view
    ->preExecute($args);
  $view
    ->build();
  if ($view
    ->getQuery() instanceof SearchApiQuery) {
    $total_rows = $view->query
      ->getSearchApiQuery()
      ->range(NULL, 1)
      ->execute()
      ->getResultCount();
  }
  else {
    $count_query_results = $view->query
      ->query()
      ->countQuery()
      ->execute();
    $total_rows = (int) $count_query_results
      ->fetchField();
  }

  // If export limit is set and the number of rows is greater than the
  // limit, then set the total to limit.
  if ($export_limit && $export_limit < $total_rows) {
    $total_rows = $export_limit;
  }

  // Get view exposed input which is the query string parameters from url.
  $query_parameters = $view
    ->getExposedInput();

  // Remove the file format parameter from the query string.
  if (array_key_exists('_format', $query_parameters)) {
    unset($query_parameters['_format']);
  }

  // Check where to redirect the user after the batch finishes.
  // Defaults to the <front> route.
  $redirect_url = Url::fromRoute('<front>');

  // Get options set in views display configuration.
  $custom_redirect = $view
    ->getDisplay()
    ->getOption('custom_redirect_path');
  $redirect_to_display = $view
    ->getDisplay()
    ->getOption('redirect_to_display');

  // Check if the url query string should be added to the redirect URL.
  $include_query_params = $view->display_handler
    ->getOption('include_query_params');
  if ($custom_redirect) {
    $redirect_path = $view->display_handler
      ->getOption('redirect_path');
    if (isset($redirect_path)) {

      // Replace tokens in the redirect_path.
      $token_service = \Drupal::token();
      $redirect_path = $token_service
        ->replace($redirect_path, [
        'view' => $view,
      ]);
      if ($include_query_params) {
        $redirect_url = Url::fromUserInput(trim($redirect_path), [
          'query' => $query_parameters,
        ]);
      }
      else {
        $redirect_url = Url::fromUserInput(trim($redirect_path));
      }
    }
  }
  elseif (isset($redirect_to_display) && $redirect_to_display !== 'none') {

    // Get views display URL.
    $display_route = $view
      ->getUrl([], $redirect_to_display)
      ->getRouteName();
    if ($include_query_params) {
      $redirect_url = Url::fromRoute($display_route, [], [
        'query' => $query_parameters,
      ]);
    }
    else {
      $redirect_url = Url::fromRoute($display_route);
    }
  }
  $batch_definition = [
    'operations' => [
      [
        [
          static::class,
          'processBatch',
        ],
        [
          $view
            ->id(),
          $view->current_display,
          $view->args,
          $view
            ->getExposedInput(),
          $total_rows,
          $query_parameters,
          $redirect_url
            ->toString(),
        ],
      ],
    ],
    'title' => t('Exporting data...'),
    'progressive' => TRUE,
    'progress_message' => t('@percentage% complete. Time elapsed: @elapsed'),
    'finished' => [
      static::class,
      'finishBatch',
    ],
  ];
  batch_set($batch_definition);
  return batch_process();
}