function EntityResource::buildWrappedResponse

Same name and namespace in other branches
  1. 8.9.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::buildWrappedResponse()
  2. 10 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::buildWrappedResponse()
  3. 11.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::buildWrappedResponse()

Builds a response with the appropriate wrapped document.

Parameters

\Drupal\jsonapi\JsonApiResource\TopLevelDataInterface $data: The data to wrap.

\Symfony\Component\HttpFoundation\Request $request: The request object.

\Drupal\jsonapi\JsonApiResource\IncludedData $includes: The resources to be included in the document. Use NullData if there should be no included resources in the document.

int $response_code: The response code.

array $headers: An array of response headers.

\Drupal\jsonapi\JsonApiResource\LinkCollection $links: The URLs to which to link. A 'self' link is added automatically.

array $meta: (optional) The top-level metadata.

Return value

\Drupal\jsonapi\ResourceResponse The response.

6 calls to EntityResource::buildWrappedResponse()
EntityResource::createIndividual in core/modules/jsonapi/src/Controller/EntityResource.php
Creates an individual entity.
EntityResource::getIndividual in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the individual entity.
EntityResource::getRelated in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the related resource.
EntityResource::getRelationship in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the relationship of an entity.
EntityResource::patchIndividual in core/modules/jsonapi/src/Controller/EntityResource.php
Patches an individual entity.

... See full list

File

core/modules/jsonapi/src/Controller/EntityResource.php, line 1014

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function buildWrappedResponse(TopLevelDataInterface $data, Request $request, IncludedData $includes, $response_code = 200, array $headers = [], LinkCollection $links = NULL, array $meta = []) {
    $links = $links ?: new LinkCollection([]);
    if (!$links->hasLinkWithKey('self')) {
        $self_link = new Link(new CacheableMetadata(), self::getRequestLink($request), 'self');
        $links = $links->withLink('self', $self_link);
    }
    $document = new JsonApiDocumentTopLevel($data, $includes, $links, $meta);
    if (!$request->isMethodCacheable()) {
        return new ResourceResponse($document, $response_code, $headers);
    }
    $response = new CacheableResourceResponse($document, $response_code, $headers);
    $cacheability = (new CacheableMetadata())->addCacheContexts([
        // Make sure that different sparse fieldsets are cached differently.
'url.query_args:fields',
        // Make sure that different sets of includes are cached differently.
'url.query_args:include',
    ]);
    $response->addCacheableDependency($cacheability);
    return $response;
}

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