You are here

public function CacheabilityCapturingExecutor::executeQueryAndCaptureCacheability in JSON:API Resources 8

Executes the query in a render context, to catch bubbled cacheability.

@todo Remove this after https://www.drupal.org/project/drupal/issues/3028976 is fixed.

Parameters

\Drupal\Core\Entity\Query\QueryInterface $query: The query to execute to get the return results.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheable_metadata: An refinable cacheable dependency with which to capture cacheability.

Return value

int|array Returns an integer for count queries or an array of IDs. The values of the array are always entity IDs. The keys will be revision IDs if the entity supports revision and entity IDs if not.

See also

node_query_node_access_alter()

https://www.drupal.org/project/drupal/issues/2557815

https://www.drupal.org/project/drupal/issues/2794385

File

src/Unstable/Entity/Query/CacheabilityCapturingExecutor.php, line 51

Class

CacheabilityCapturingExecutor
Executes entity queries and captures cacheability.

Namespace

Drupal\jsonapi_resources\Unstable\Entity\Query

Code

public function executeQueryAndCaptureCacheability(QueryInterface $query, RefinableCacheableDependencyInterface $cacheable_metadata) {
  $context = new RenderContext();
  $results = $this->renderer
    ->executeInRenderContext($context, function () use ($query) {
    return $query
      ->execute();
  });
  $paginator_metadata = $query
    ->getMetaData(PaginatorMetadata::KEY);
  if (is_array($results) && $paginator_metadata instanceof PaginatorMetadata && count($results) > (int) $paginator_metadata->pageSizeMax) {
    $paginator_metadata->hasNextPage = TRUE;
    array_pop($results);
  }
  if (!$context
    ->isEmpty()) {
    $cacheable_metadata
      ->addCacheableDependency($context
      ->pop());
  }
  return $results;
}