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\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
|
|
|
* @GraphQLField( |
18
|
|
|
* id = "entity_revision_by_id", |
19
|
|
|
* secure = true, |
20
|
|
|
* arguments = { |
21
|
|
|
* "id" = "String!" |
22
|
|
|
* }, |
23
|
|
|
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRevisionByIdDeriver" |
24
|
|
|
* ) |
25
|
|
|
*/ |
26
|
|
|
class EntityRevisionById extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
27
|
|
|
use DependencySerializationTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The entity type manager service. |
31
|
|
|
* |
32
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $entityTypeManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The entity repository service. |
38
|
|
|
* |
39
|
|
|
* @var \Drupal\Core\Entity\EntityRepositoryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $entityRepository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
array $configuration, |
48
|
|
|
$pluginId, |
49
|
|
|
$pluginDefinition, |
50
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
51
|
|
|
EntityRepositoryInterface $entityRepository |
52
|
|
|
) { |
53
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
54
|
|
|
$this->entityTypeManager = $entityTypeManager; |
55
|
|
|
$this->entityRepository = $entityRepository; |
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
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
75
|
|
|
$definition = $this->getPluginDefinition(); |
76
|
|
|
$storage = $this->entityTypeManager->getStorage($definition['entity_type']); |
77
|
|
|
|
78
|
|
|
if (!$entity = $storage->loadRevision($args['id'])) { |
79
|
|
|
// If there is no entity with this id, add the list cache tags so that the |
80
|
|
|
// cache entry is purged whenever a new entity of this type is saved. |
81
|
|
|
$pluginDefinition = $this->getPluginDefinition(); |
82
|
|
|
$entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']); |
83
|
|
|
$metadata = new CacheableMetadata(); |
84
|
|
|
$metadata->addCacheTags($entityType->getListCacheTags()); |
85
|
|
|
|
86
|
|
|
yield new CacheableValue(NULL, [$metadata]); |
87
|
|
|
} |
88
|
|
|
/** @var \Drupal\Core\Access\AccessResultInterface $access */ |
89
|
|
View Code Duplication |
else if (($access = $entity->access('view', NULL, TRUE)) && $access->isAllowed()) { |
|
|
|
|
90
|
|
|
if (isset($args['language']) && $args['language'] != $entity->language()->getId()) { |
91
|
|
|
$entity = $this->entityRepository->getTranslationFromContext($entity, $args['language']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
yield new CacheableValue($entity, [$access]); |
95
|
|
|
} |
96
|
|
|
else { |
97
|
|
|
// If the entity exists but we do not grant access to it, we still want |
98
|
|
|
// to have it's cache metadata in the output because future changes to |
99
|
|
|
// the entity might affect its visibility for the user. |
100
|
|
|
yield new CacheableValue(NULL, [$access]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|