Skip to content

Commit 73d02ee

Browse files
committed
Support sorts.
1 parent 088d511 commit 73d02ee

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Plugin/GraphQL/DataProducer/Entity/EntityQuery.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
* label = @Translation("Entity bundle(s)"),
6262
* multiple = TRUE,
6363
* required = FALSE
64+
* ),
65+
* "sorts" = @ContextDefinition("any",
66+
* label = @Translation("Sorts"),
67+
* multiple = TRUE,
68+
* default_value = {},
69+
* required = FALSE
6470
* )
6571
* }
6672
* )
@@ -139,6 +145,8 @@ public function __construct(
139145
* Language of queried entities.
140146
* @param array|null $bundles
141147
* List of bundles to be filtered.
148+
* @param array|null $sorts
149+
* List of sorts.
142150
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
143151
* The caching context related to the current field.
144152
*
@@ -148,7 +156,7 @@ public function __construct(
148156
* @throws \GraphQL\Error\UserError
149157
* No bundles defined for given entity type.
150158
*/
151-
public function resolve(string $type, int $limit = 10, ?int $offset, ?bool $ownedOnly, ?array $conditions, ?array $allowedFilters, ?string $language, ?array $bundles, FieldContext $context): array {
159+
public function resolve(string $type, int $limit = 10, ?int $offset, ?bool $ownedOnly, ?array $conditions, ?array $allowedFilters, ?string $language, ?array $bundles, ?array $sorts, FieldContext $context): array {
152160
// Make sure offset is zero or positive.
153161
$offset = max($offset ?: 0, 0);
154162

@@ -188,6 +196,18 @@ public function resolve(string $type, int $limit = 10, ?int $offset, ?bool $owne
188196
$query->condition($condition['field'], $condition['value'], $operation);
189197
}
190198

199+
foreach ($sorts as $sort) {
200+
if (!empty($sort['field'])) {
201+
if (!empty($sort['direction']) && strtolower($sort['direction']) == 'desc') {
202+
$direction = 'DESC';
203+
}
204+
else {
205+
$direction = 'ASC';
206+
}
207+
$query->sort($sort['field'], $direction);
208+
}
209+
}
210+
191211
$ids = $query->execute();
192212

193213
return $ids;

0 commit comments

Comments
 (0)