function EntityResource::getPagerLinks

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

Get the pager links for a given request object.

Parameters

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

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

array $link_context: An associative array with extra data to build the links.

Return value

\Drupal\jsonapi\JsonApiResource\LinkCollection A LinkCollection, with:

  • a 'next' key if it is not the last page;
  • 'prev' and 'first' keys if it's not the first page.
1 call to EntityResource::getPagerLinks()
EntityResource::respondWithCollection in core/modules/jsonapi/src/Controller/EntityResource.php
Respond with an entity collection.

File

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

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected static function getPagerLinks(Request $request, OffsetPage $page_param, array $link_context = []) {
    $pager_links = new LinkCollection([]);
    if (!empty($link_context['total_count']) && !($total = (int) $link_context['total_count'])) {
        return $pager_links;
    }
    $offset = $page_param->getOffset();
    $size = $page_param->getSize();
    if ($size <= 0) {
        $cacheability = (new CacheableMetadata())->addCacheContexts([
            'url.query_args:page',
        ]);
        throw new CacheableBadRequestHttpException($cacheability, sprintf('The page size needs to be a positive integer.'));
    }
    $query = (array) $request->query
        ->getIterator();
    // Check if this is not the last page.
    if ($link_context['has_next_page']) {
        $next_url = static::getRequestLink($request, static::getPagerQueries('next', $offset, $size, $query));
        $pager_links = $pager_links->withLink('next', new Link(new CacheableMetadata(), $next_url, 'next'));
        if (!empty($total)) {
            $last_url = static::getRequestLink($request, static::getPagerQueries('last', $offset, $size, $query, $total));
            $pager_links = $pager_links->withLink('last', new Link(new CacheableMetadata(), $last_url, 'last'));
        }
    }
    // Check if this is not the first page.
    if ($offset > 0) {
        $first_url = static::getRequestLink($request, static::getPagerQueries('first', $offset, $size, $query));
        $pager_links = $pager_links->withLink('first', new Link(new CacheableMetadata(), $first_url, 'first'));
        $prev_url = static::getRequestLink($request, static::getPagerQueries('prev', $offset, $size, $query));
        $pager_links = $pager_links->withLink('prev', new Link(new CacheableMetadata(), $prev_url, 'prev'));
    }
    return $pager_links;
}

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