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\Core\TypedData\TranslatableInterface; |
|
|
|
|
10
|
|
|
use Drupal\graphql\GraphQL\Buffers\EntityBuffer; |
11
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
12
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
13
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
|
15
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "entity_by_id", |
20
|
|
|
* secure = true, |
21
|
|
|
* arguments = { |
22
|
|
|
* "id" = "String!" |
23
|
|
|
* }, |
24
|
|
|
* contextual_arguments = {"language"}, |
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 static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
56
|
|
|
return new static( |
57
|
|
|
$configuration, |
58
|
|
|
$pluginId, |
59
|
|
|
$pluginDefinition, |
60
|
|
|
$container->get('entity_type.manager'), |
61
|
|
|
$container->get('entity.repository'), |
62
|
|
|
$container->get('graphql.buffer.entity') |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* EntityById constructor. |
68
|
|
|
* |
69
|
|
|
* @param array $configuration |
70
|
|
|
* The plugin configuration array. |
71
|
|
|
* @param string $pluginId |
72
|
|
|
* The plugin id. |
73
|
|
|
* @param mixed $pluginDefinition |
74
|
|
|
* The plugin definition. |
75
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
76
|
|
|
* The entity type manager service. |
77
|
|
|
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository |
78
|
|
|
* The entity repository service. |
79
|
|
|
* @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer |
80
|
|
|
* The entity buffer service. |
81
|
|
|
*/ |
82
|
|
|
public function __construct( |
83
|
|
|
array $configuration, |
84
|
|
|
$pluginId, |
85
|
|
|
$pluginDefinition, |
86
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
87
|
|
|
EntityRepositoryInterface $entityRepository, |
88
|
|
|
EntityBuffer $entityBuffer |
89
|
|
|
) { |
90
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
91
|
|
|
$this->entityBuffer = $entityBuffer; |
92
|
|
|
$this->entityTypeManager = $entityTypeManager; |
93
|
|
|
$this->entityRepository = $entityRepository; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
100
|
|
|
$resolver = $this->entityBuffer->add($this->getPluginDefinition()['entity_type'], $args['id']); |
101
|
|
|
return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolver) { |
|
|
|
|
102
|
|
|
if (!$entity = $resolver()) { |
103
|
|
|
// If there is no entity with this id, add the list cache tags so that the |
104
|
|
|
// cache entry is purged whenever a new entity of this type is saved. |
105
|
|
|
$pluginDefinition = $this->getPluginDefinition(); |
106
|
|
|
$entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']); |
107
|
|
|
yield (new CacheableValue(NULL))->addCacheTags($entityType->getListCacheTags()); |
108
|
|
|
} |
109
|
|
|
else { |
110
|
|
|
/** @var \Drupal\Core\Entity\EntityInterface $entity */ |
111
|
|
|
$access = $entity->access('view', NULL, TRUE); |
112
|
|
|
|
113
|
|
|
if ($access->isAllowed()) { |
114
|
|
|
if (isset($args['language']) && $args['language'] != $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->isTranslatable()) { |
115
|
|
|
if ($entity->hasTranslation($args['language'])) { |
116
|
|
|
$entity = $entity->getTranslation($args['language']); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
yield $entity->addCacheableDependency($access); |
121
|
|
|
} |
122
|
|
|
else { |
123
|
|
|
yield new CacheableValue(NULL, [$access]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths