protected function EntityResource::getCollectionQuery in JSON:API 8
Same name and namespace in other branches
- 8.2 src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::getCollectionQuery()
Gets a basic query for a collection.
Parameters
string $entity_type_id: The entity type for the entity 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.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
1 call to EntityResource::getCollectionQuery()
- EntityResource::getCollection in src/
Controller/ EntityResource.php - Gets the collection of entities.
File
- src/
Controller/ EntityResource.php, line 812
Class
- EntityResource
- Process all entity requests.
Namespace
Drupal\jsonapi\ControllerCode
protected function getCollectionQuery($entity_type_id, array $params, CacheableMetadata $query_cacheability) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$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 = $field[Sort::PATH_KEY];
$direction = isset($field[Sort::DIRECTION_KEY]) ? $field[Sort::DIRECTION_KEY] : 'ASC';
$langcode = isset($field[Sort::LANGUAGE_KEY]) ? $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 = $this->resourceType
->getBundle();
if ($bundle && ($bundle_key = $entity_type
->getKey('bundle'))) {
$query
->condition($bundle_key, $bundle);
}
return $query;
}