|
| 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