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