Example #1
0
 /**
  * {@inheritdoc}
  */
 public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match)
 {
     $json = [];
     $json['content'] = (string) $this->renderer->renderRoot($main_content);
     if (!empty($main_content['#title'])) {
         $json['title'] = (string) $main_content['#title'];
     } else {
         $json['title'] = (string) $this->titleResolver->getTitle($request, $route_match->getRouteObject());
     }
     $response = new CacheableJsonResponse($json, 200);
     $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($main_content));
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public static function buildResponse($view_id, $display_id, array $args = [])
 {
     $build = static::buildBasicRenderable($view_id, $display_id, $args);
     $build['#cache']['contexts'][] = "url.query_args:callback";
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $output = $renderer->renderRoot($build);
     if (isset($build['#jsonp_callback'])) {
         $response = new CacheableJsonResponse($output, 200);
         $response->setCallback($build['#jsonp_callback']);
     } else {
         $response = new CacheableResponse($output, 200);
     }
     $cache_metadata = CacheableMetadata::createFromRenderArray($build);
     $response->addCacheableDependency($cache_metadata);
     $response->headers->set('Content-type', $build['#content_type']);
     return $response;
 }