Skip to content

Commit 758c5ed

Browse files
authored
Add exclusive entity query field. (#517)
1 parent a804f1d commit 758c5ed

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Enums\EntityQuery;
4+
5+
use Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase;
6+
7+
/**
8+
* @GraphQLEnum(
9+
* id = "entity_query_bundle_mode",
10+
* name = "EntityQueryBundleMode",
11+
* values = {
12+
* "same" = {
13+
* "name" = "same",
14+
* "description" = @Translation("Loads only entities that share the same bundle with the parent entity."),
15+
* },
16+
* "all" = {
17+
* "name" = "all",
18+
* "description" = @Translation("Loads entities across all bundles."),
19+
* }
20+
* }
21+
* )
22+
*/
23+
class EntityQueryBundleMode extends EnumPluginBase {
24+
25+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4+
5+
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6+
use Drupal\Core\Entity\ContentEntityInterface;
7+
use Drupal\Core\Entity\Query\QueryInterface;
8+
use Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery\EntityQuery;
9+
use Youshido\GraphQL\Execution\ResolveInfo;
10+
11+
/**
12+
* Query entities of the same type without the context's entity.
13+
*
14+
* @GraphQLField(
15+
* id = "entity_query_exclusive",
16+
* name = "entityQueryExclusive",
17+
* secure = true,
18+
* type = "EntityQueryResult!",
19+
* parents = {"Entity"},
20+
* arguments = {
21+
* "filter" = "EntityQueryFilterInput",
22+
* "sort" = "[EntityQuerySortInput]",
23+
* "offset" = {
24+
* "type" = "Int",
25+
* "default" = 0
26+
* },
27+
* "limit" = {
28+
* "type" = "Int",
29+
* "default" = 10
30+
* },
31+
* "revisions" = {
32+
* "type" = "EntityQueryRevisionMode",
33+
* "default" = "default"
34+
* },
35+
* "bundles" = {
36+
* "type" = "EntityQueryBundleMode",
37+
* "default" = "same"
38+
* }
39+
* }
40+
* )
41+
*/
42+
class EntityQueryExclusive extends EntityQuery {
43+
use DependencySerializationTrait;
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function getBaseQuery($value, array $args, ResolveInfo $info) {
49+
if ($value instanceof ContentEntityInterface) {
50+
$type = $value->getEntityType();
51+
$id = $type->getKey('id');
52+
53+
// Filter out the current entity.
54+
$query = parent::getBaseQuery($value, $args, $info);
55+
$query->condition($id, $value->id(), '<>');
56+
57+
if (array_key_exists('bundles', $args)) {
58+
$query = $this->applyBundleMode($query, $value, $args['bundles']);
59+
}
60+
61+
return $query;
62+
}
63+
}
64+
65+
/**
66+
* Apply the specified bundle filtering mode to the query.
67+
*
68+
* @param \Drupal\Core\Entity\Query\QueryInterface $query
69+
* The entity query object.
70+
* @param \Drupal\Core\Entity\ContentEntityInterface $value
71+
* The parent entity object.
72+
* @param mixed $mode
73+
* The revision query mode.
74+
*
75+
* @return \Drupal\Core\Entity\Query\QueryInterface The entity query object.
76+
* The entity query object.
77+
*/
78+
protected function applyBundleMode(QueryInterface $query, ContentEntityInterface $value, $mode) {
79+
if ($mode === 'same') {
80+
$type = $value->getEntityType();
81+
82+
if ($type->hasKey('bundle')) {
83+
$bundle = $type->getKey('bundle');
84+
$query->condition($bundle, $value->bundle());
85+
}
86+
}
87+
88+
return $query;
89+
}
90+
91+
}

0 commit comments

Comments
 (0)