You are here

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

Builds standard export response.

Parameters

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

Return value

\Drupal\Core\Cache\CacheableResponse Redirect to the batching page.

1 call to DataExport::buildStandard()
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 168

Class

DataExport
Provides a data export display plugin.

Namespace

Drupal\views_data_export\Plugin\views\display

Code

protected static function buildStandard(ViewExecutable $view) {
  $build = $view
    ->buildRenderable();

  // Setup an empty response so headers can be added as needed during views
  // rendering and processing.
  $response = new CacheableResponse('', 200);
  $build['#response'] = $response;

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $output = (string) $renderer
    ->renderRoot($build);
  $response
    ->setContent($output);
  $cache_metadata = CacheableMetadata::createFromRenderArray($build);
  $response
    ->addCacheableDependency($cache_metadata);

  // Set filename if such exists.
  if ($filename = $view
    ->getDisplay()
    ->getOption('filename')) {
    $bubbleable_metadata = BubbleableMetadata::createFromObject($cache_metadata);
    $response->headers
      ->set('Content-Disposition', 'attachment; filename="' . \Drupal::token()
      ->replace($filename, [
      'view' => $view,
    ], [], $bubbleable_metadata) . '"');
  }
  $response->headers
    ->set('Content-type', $build['#content_type']);
  return $response;
}