function EntityResource::getCollectionQuery

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

Gets a basic query for a collection.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The base JSON:API resource type for the query.

array $params: The parameters for the query.

\Drupal\Core\Cache\CacheableMetadata $query_cacheability: Collects cacheability for the query.

Return value

\Drupal\Core\Entity\Query\QueryInterface A new query.

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

File

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

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function getCollectionQuery(ResourceType $resource_type, array $params, CacheableMetadata $query_cacheability) {
    $entity_type = $this->entityTypeManager
        ->getDefinition($resource_type->getEntityTypeId());
    $entity_storage = $this->entityTypeManager
        ->getStorage($resource_type->getEntityTypeId());
    $query = $entity_storage->getQuery();
    // Ensure that access checking is performed on the query.
    $query->accessCheck(TRUE);
    // Compute and apply an entity query condition from the filter parameter.
    if (isset($params[Filter::KEY_NAME]) && ($filter = $params[Filter::KEY_NAME])) {
        $query->condition($filter->queryCondition($query));
        TemporaryQueryGuard::setFieldManager($this->fieldManager);
        TemporaryQueryGuard::setModuleHandler(\Drupal::moduleHandler());
        TemporaryQueryGuard::applyAccessControls($filter, $query, $query_cacheability);
    }
    // Apply any sorts to the entity query.
    if (isset($params[Sort::KEY_NAME]) && ($sort = $params[Sort::KEY_NAME])) {
        foreach ($sort->fields() as $field) {
            $path = $this->fieldResolver
                ->resolveInternalEntityQueryPath($resource_type, $field[Sort::PATH_KEY]);
            $direction = $field[Sort::DIRECTION_KEY] ?? 'ASC';
            $langcode = $field[Sort::LANGUAGE_KEY] ?? NULL;
            $query->sort($path, $direction, $langcode);
        }
    }
    // Apply any pagination options to the query.
    if (isset($params[OffsetPage::KEY_NAME])) {
        $pagination = $params[OffsetPage::KEY_NAME];
    }
    else {
        $pagination = new OffsetPage(OffsetPage::DEFAULT_OFFSET, OffsetPage::SIZE_MAX);
    }
    // Add one extra element to the page to see if there are more pages needed.
    $query->range($pagination->getOffset(), $pagination->getSize() + 1);
    $query->addMetaData('pager_size', (int) $pagination->getSize());
    // Limit this query to the bundle type for this resource.
    $bundle = $resource_type->getBundle();
    if ($bundle && ($bundle_key = $entity_type->getKey('bundle'))) {
        $query->condition($bundle_key, $bundle);
    }
    return $query;
}

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