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