You are here

public static function RestExport::buildResponse in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/src/Plugin/views/display/RestExport.php \Drupal\rest\Plugin\views\display\RestExport::buildResponse()

Builds up a response with the rendered view as content.

Parameters

string $view_id: The view ID.

string $display_id: The display ID.

array $args: (optional) The arguments of the view.

Return value

\Symfony\Component\HttpFoundation\Response The built response.

Overrides ResponseDisplayPluginInterface::buildResponse

1 call to RestExport::buildResponse()
RestExportTest::testBuildResponse in core/modules/rest/tests/src/Kernel/Views/RestExportTest.php
@covers ::buildResponse

File

core/modules/rest/src/Plugin/views/display/RestExport.php, line 413

Class

RestExport
The plugin that handles Data response callbacks for REST resources.

Namespace

Drupal\rest\Plugin\views\display

Code

public static function buildResponse($view_id, $display_id, array $args = []) {
  $build = static::buildBasicRenderable($view_id, $display_id, $args);

  // 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);
  $response->headers
    ->set('Content-type', $build['#content_type']);
  return $response;
}