You are here

public function JsonRenderer::renderResponse in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/common_test/src/Render/MainContent/JsonRenderer.php \Drupal\common_test\Render\MainContent\JsonRenderer::renderResponse()
  2. 10 core/modules/system/tests/modules/common_test/src/Render/MainContent/JsonRenderer.php \Drupal\common_test\Render\MainContent\JsonRenderer::renderResponse()

Renders the main content render array into a response.

Parameters

array $main_content: The render array representing the main content.

\Symfony\Component\HttpFoundation\Request $request: The request object, for context.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match, for context.

Return value

\Symfony\Component\HttpFoundation\Response The Response in the format that this implementation supports.

Overrides MainContentRendererInterface::renderResponse

File

core/modules/system/tests/modules/common_test/src/Render/MainContent/JsonRenderer.php, line 48

Class

JsonRenderer
Default main content renderer for JSON requests.

Namespace

Drupal\common_test\Render\MainContent

Code

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;
}