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
|
|
|
* @var \Drupal\graphql\GraphQL\Batching\Buffers\EntityBuffer |
37
|
|
|
*/ |
38
|
|
|
protected $entityBuffer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
42
|
|
|
*/ |
43
|
|
|
protected $entityTypeManager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \Drupal\Core\Entity\EntityRepositoryInterface |
47
|
|
|
*/ |
48
|
|
|
protected $entityRepository; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
array $configuration, |
55
|
|
|
$pluginId, |
56
|
|
|
$pluginDefinition, |
57
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
58
|
|
|
EntityRepositoryInterface $entityRepository, |
59
|
|
|
EntityBuffer $entityBuffer |
60
|
|
|
) { |
61
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
62
|
|
|
$this->entityBuffer = $entityBuffer; |
63
|
|
|
$this->entityTypeManager = $entityTypeManager; |
64
|
|
|
$this->entityRepository = $entityRepository; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
71
|
|
|
return new static( |
72
|
|
|
$configuration, |
73
|
|
|
$pluginId, |
74
|
|
|
$pluginDefinition, |
75
|
|
|
$container->get('entity_type.manager'), |
76
|
|
|
$container->get('entity.repository'), |
77
|
|
|
$container->get('graphql.buffer.entity') |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
85
|
|
|
$resolver = $this->entityBuffer->add($this->getPluginDefinition()['entity_type'], $args['id']); |
86
|
|
|
return function ($value, array $args, ResolveInfo $info) use ($resolver) { |
|
|
|
|
87
|
|
|
if (!$entity = $resolver()) { |
88
|
|
|
// If there is no entity with this id, add the list cache tags so that the |
89
|
|
|
// cache entry is purged whenever a new entity of this type is saved. |
90
|
|
|
$pluginDefinition = $this->getPluginDefinition(); |
91
|
|
|
$entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']); |
92
|
|
|
$metadata = new CacheableMetadata(); |
93
|
|
|
$metadata->addCacheTags($entityType->getListCacheTags()); |
94
|
|
|
|
95
|
|
|
yield new CacheableValue(NULL, [$metadata]); |
96
|
|
|
} |
97
|
|
|
/** @var \Drupal\Core\Access\AccessResultInterface $access */ |
98
|
|
|
else if (($access = $entity->access('view', NULL, TRUE)) && $access->isAllowed()) { |
99
|
|
|
if (isset($args['language']) && $args['language'] != $entity->language()->getId()) { |
100
|
|
|
$entity = $this->entityRepository->getTranslationFromContext($entity, $args['language']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
yield new CacheableValue($entity, [$access]); |
104
|
|
|
} |
105
|
|
|
else { |
106
|
|
|
// If the entity exists but we do not grant access to it, we still want |
107
|
|
|
// to have it's cache metadata in the output because future changes to |
108
|
|
|
// the entity might affect its visibility for the user. |
109
|
|
|
yield new CacheableValue(NULL, [$access]); |
110
|
|
|
} |
111
|
|
|
}; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|