You are here

public static function JsonFeed::buildResponse in JSON Feed 8

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 Feed::buildResponse

File

src/Plugin/views/display/JsonFeed.php, line 59

Class

JsonFeed
The plugin that handles a JSON feed.

Namespace

Drupal\json_feed\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;
}