You are here

public function OffsetLimitPaginator::applyToQuery in JSON:API Resources 8

Modifies an entity query.

Parameters

\Drupal\Core\Entity\Query\QueryInterface $query: The query to be modified.

\Drupal\Core\Cache\CacheableMetadata $cacheable_metadata: A CacheableMetadata object that will be used to capture any cacheability information generated by the modifier. The same object that is passed to this method should be added to the cacheability of the final response by the caller.

Overrides PaginatorInterface::applyToQuery

File

src/Unstable/Entity/Query/Pagination/OffsetLimitPaginator.php, line 79

Class

OffsetLimitPaginator
A paginator for handling offset-limit pagination in JSON:API request.

Namespace

Drupal\jsonapi_resources\Unstable\Entity\Query\Pagination

Code

public function applyToQuery(QueryInterface $query, CacheableMetadata $cacheable_metadata) : void {

  // Ensure that different pages will be cached separately.
  $cacheable_metadata
    ->addCacheContexts([
    'url.query_args:page',
  ]);

  // Derive any pagination options from the query params or use defaults.
  $pagination = $this->request->query
    ->has('page') ? OffsetPage::createFromQueryParameter($this->request->query
    ->get('page')) : new OffsetPage(OffsetPage::DEFAULT_OFFSET, OffsetPage::SIZE_MAX);
  $query
    ->range($pagination
    ->getOffset(), $pagination
    ->getSize() + 1);
  $metadata = new PaginatorMetadata();
  $metadata->pageSizeMax = $pagination
    ->getSize();
  $metadata->pageLocation = $pagination
    ->getOffset();
  $query
    ->addMetaData(PaginatorMetadata::KEY, $metadata);
}