You are here

protected function EntityResource::respondWithCollection in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::respondWithCollection()

Respond with an entity collection.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObjectData $primary_data: The collection of entities.

\Drupal\jsonapi\JsonApiResource\IncludedData|\Drupal\jsonapi\JsonApiResource\NullIncludedData $includes: The resources to be included in the document.

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

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The base JSON:API resource type for the request to be served.

\Drupal\jsonapi\Query\OffsetPage $page_param: The pagination parameter for the requested collection.

Return value

\Drupal\jsonapi\ResourceResponse The response.

1 call to EntityResource::respondWithCollection()
EntityResource::getCollection in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the collection of entities.

File

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

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function respondWithCollection(ResourceObjectData $primary_data, Data $includes, Request $request, ResourceType $resource_type, OffsetPage $page_param) {
  assert(Inspector::assertAllObjects([
    $includes,
  ], IncludedData::class, NullIncludedData::class));
  $link_context = [
    'has_next_page' => $primary_data
      ->hasNextPage(),
  ];
  $meta = [];
  if ($resource_type
    ->includeCount()) {
    $link_context['total_count'] = $meta['count'] = $primary_data
      ->getTotalCount();
  }
  $collection_links = self::getPagerLinks($request, $page_param, $link_context);
  $response = $this
    ->buildWrappedResponse($primary_data, $request, $includes, 200, [], $collection_links, $meta);

  // When a new change to any entity in the resource happens, we cannot ensure
  // the validity of this cached list. Add the list tag to deal with that.
  $list_tag = $this->entityTypeManager
    ->getDefinition($resource_type
    ->getEntityTypeId())
    ->getListCacheTags();
  $response
    ->getCacheableMetadata()
    ->addCacheTags($list_tag);
  foreach ($primary_data as $entity) {
    $response
      ->addCacheableDependency($entity);
  }
  return $response;
}