You are here

protected function IndexResource::getPagerLinks in JSON:API Search API 8

Get pager links.

Parameters

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

\Drupal\jsonapi\Query\OffsetPage $pagination: The pagination object.

int $total_count: The total count.

int $result_count: The result count.

Return value

\Drupal\jsonapi\JsonApiResource\LinkCollection The link collection.

1 call to IndexResource::getPagerLinks()
IndexResource::process in src/Resource/IndexResource.php
Process the resource request.

File

src/Resource/IndexResource.php, line 219

Class

IndexResource
JSON:API Resource to return Search API index results.

Namespace

Drupal\jsonapi_search_api\Resource

Code

protected function getPagerLinks(Request $request, OffsetPage $pagination, int $total_count, int $result_count) : LinkCollection {
  $pager_links = new LinkCollection([]);
  $size = (int) $pagination
    ->getSize();
  $offset = $pagination
    ->getOffset();
  $query = (array) $request->query
    ->getIterator();

  // Check if this is not the last page.
  if ($pagination
    ->getOffset() + $result_count < $total_count) {
    $next_url = static::getRequestLink($request, static::getPagerQueries('next', $offset, $size, $query));
    $pager_links = $pager_links
      ->withLink('next', new Link(new CacheableMetadata(), $next_url, 'next'));
    $last_url = static::getRequestLink($request, static::getPagerQueries('last', $offset, $size, $query, $total_count));
    $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;
}