You are here

protected function ThunderEntityListProducerBase::query in Thunder 6.2.x

Build base entity query which may be reused for count query as well.

Parameters

string $type: Entity type.

string[] $bundles: List of bundles to be filtered.

int $offset: Query only entities owned by current user.

int $limit: Maximum number of queried entities.

array $conditions: List of conditions to filter the entities.

string[] $languages: Languages for queried entities.

array $sortBy: List of sorts.

\Drupal\graphql\GraphQL\Execution\FieldContext $cacheContext: The caching context related to the current field.

Return value

\Drupal\Core\Entity\Query\QueryInterface The query interface.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to ThunderEntityListProducerBase::query()
EntitiesWithTerm::resolve in modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntitiesWithTerm.php
Build entity query for entities, that reference a specific term.
ThunderEntityList::resolve in modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php
Resolve entity query.

File

modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php, line 100

Class

ThunderEntityListProducerBase
The thunder base class for entity list producers.

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\DataProducer

Code

protected function query(string $type, array $bundles, int $offset, int $limit, array $conditions, array $languages, array $sortBy, FieldContext $cacheContext) : QueryInterface {
  if ($limit > static::MAX_ITEMS) {
    throw new UserError(sprintf('Exceeded maximum query limit: %s.', static::MAX_ITEMS));
  }
  $entity_type = $this->entityTypeManager
    ->getStorage($type);
  $query = $entity_type
    ->getQuery();
  $query
    ->currentRevision()
    ->accessCheck();

  // Ensure that access checking is performed on the query.
  $query
    ->currentRevision()
    ->accessCheck(TRUE);

  // Filter entities only of given bundles, if desired.
  if ($bundles) {
    $bundle_key = $entity_type
      ->getEntityType()
      ->getKey('bundle');
    if (!$bundle_key) {
      throw new UserError('No bundles defined for given entity type.');
    }
    $query
      ->condition($bundle_key, $bundles, 'IN');
  }

  // Filter entities by given languages, if desired.
  if ($languages) {
    $query
      ->condition('langcode', $languages, 'IN');
  }

  // Filter by given conditions.
  foreach ($conditions as $condition) {
    $operation = isset($condition['operator']) ? $condition['operator'] : NULL;
    $query
      ->condition($condition['field'], $condition['value'], $operation);
  }
  if (!empty($sortBy)) {
    foreach ($sortBy as $sort) {
      if (!empty($sort['field'])) {
        if (!empty($sort['direction']) && strtolower($sort['direction']) == 'desc') {
          $direction = 'DESC';
        }
        else {
          $direction = 'ASC';
        }
        $query
          ->sort($sort['field'], $direction);
      }
    }
  }
  $query
    ->range($offset, $limit);
  $storage = $this->entityTypeManager
    ->getStorage($type);
  $entityType = $storage
    ->getEntityType();
  $cacheContext
    ->addCacheTags($entityType
    ->getListCacheTags());
  $cacheContext
    ->addCacheContexts($entityType
    ->getListCacheContexts());
  return $query;
}