1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
6
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
7
|
|
|
use Drupal\Core\Entity\EntityRepositoryInterface; |
8
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
9
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
10
|
|
|
use Drupal\graphql\GraphQL\Batching\Buffers\EntityBuffer; |
11
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Retrieve an entity by its id. |
18
|
|
|
* |
19
|
|
|
* @GraphQLField( |
20
|
|
|
* id = "entity_by_id", |
21
|
|
|
* secure = true, |
22
|
|
|
* name = "entityById", |
23
|
|
|
* nullable = true, |
24
|
|
|
* multi = false, |
25
|
|
|
* weight = -1, |
26
|
|
|
* arguments = { |
27
|
|
|
* "id" = "String" |
28
|
|
|
* }, |
29
|
|
|
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityByIdDeriver" |
30
|
|
|
* ) |
31
|
|
|
*/ |
32
|
|
|
class EntityById extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
33
|
|
|
use DependencySerializationTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The entity buffer service. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\graphql\GraphQL\Batching\Buffers\EntityBuffer |
39
|
|
|
*/ |
40
|
|
|
protected $entityBuffer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The entity type manager service. |
44
|
|
|
* |
45
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
46
|
|
|
*/ |
47
|
|
|
protected $entityTypeManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The entity repository service. |
51
|
|
|
* |
52
|
|
|
* @var \Drupal\Core\Entity\EntityRepositoryInterface |
53
|
|
|
*/ |
54
|
|
|
protected $entityRepository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
array $configuration, |
61
|
|
|
$pluginId, |
62
|
|
|
$pluginDefinition, |
63
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
64
|
|
|
EntityRepositoryInterface $entityRepository, |
65
|
|
|
EntityBuffer $entityBuffer |
66
|
|
|
) { |
67
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
68
|
|
|
$this->entityBuffer = $entityBuffer; |
69
|
|
|
$this->entityTypeManager = $entityTypeManager; |
70
|
|
|
$this->entityRepository = $entityRepository; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
77
|
|
|
return new static( |
78
|
|
|
$configuration, |
79
|
|
|
$pluginId, |
80
|
|
|
$pluginDefinition, |
81
|
|
|
$container->get('entity_type.manager'), |
82
|
|
|
$container->get('entity.repository'), |
83
|
|
|
$container->get('graphql.buffer.entity') |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
91
|
|
|
$resolver = $this->entityBuffer->add($this->getPluginDefinition()['entity_type'], $args['id']); |
92
|
|
|
return function ($value, array $args, ResolveInfo $info) use ($resolver) { |
|
|
|
|
93
|
|
|
if (!$entity = $resolver()) { |
94
|
|
|
// If there is no entity with this id, add the list cache tags so that the |
95
|
|
|
// cache entry is purged whenever a new entity of this type is saved. |
96
|
|
|
$pluginDefinition = $this->getPluginDefinition(); |
97
|
|
|
$entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']); |
98
|
|
|
$metadata = new CacheableMetadata(); |
99
|
|
|
$metadata->addCacheTags($entityType->getListCacheTags()); |
100
|
|
|
|
101
|
|
|
yield new CacheableValue(NULL, [$metadata]); |
102
|
|
|
} |
103
|
|
|
/** @var \Drupal\Core\Access\AccessResultInterface $access */ |
104
|
|
|
else if (($access = $entity->access('view', NULL, TRUE)) && $access->isAllowed()) { |
105
|
|
|
if (isset($args['language']) && $args['language'] != $entity->language()->getId()) { |
106
|
|
|
$entity = $this->entityRepository->getTranslationFromContext($entity, $args['language']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
yield new CacheableValue($entity, [$access]); |
110
|
|
|
} |
111
|
|
|
else { |
112
|
|
|
// If the entity exists but we do not grant access to it, we still want |
113
|
|
|
// to have it's cache metadata in the output because future changes to |
114
|
|
|
// the entity might affect its visibility for the user. |
115
|
|
|
yield new CacheableValue(NULL, [$access]); |
116
|
|
|
} |
117
|
|
|
}; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|