function JsonRenderer::renderResponse

Same name 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. 8.9.x core/modules/system/tests/modules/common_test/src/Render/MainContent/JsonRenderer.php \Drupal\common_test\Render\MainContent\JsonRenderer::renderResponse()
  3. 11.x core/modules/system/tests/modules/common_test/src/Render/MainContent/JsonRenderer.php \Drupal\common_test\Render\MainContent\JsonRenderer::renderResponse()

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.