-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathEntityTranslation.php
125 lines (116 loc) · 4 KB
/
EntityTranslation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Entity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns the entity in a given language.
*
* @DataProducer(
* id = "entity_translation",
* name = @Translation("Entity translation"),
* description = @Translation("Returns the translated entity."),
* produces = @ContextDefinition("entity",
* label = @Translation("Translated entity")
* ),
* consumes = {
* "entity" = @ContextDefinition("entity",
* label = @Translation("Entity")
* ),
* "language" = @ContextDefinition("string",
* label = @Translation("Language")
* ),
* "access" = @ContextDefinition("boolean",
* label = @Translation("Check access"),
* required = FALSE,
* default_value = TRUE
* ),
* "access_user" = @ContextDefinition("entity:user",
* label = @Translation("User"),
* required = FALSE,
* default_value = NULL
* ),
* "access_operation" = @ContextDefinition("string",
* label = @Translation("Operation"),
* required = FALSE,
* default_value = "view"
* )
* }
* )
*/
class EntityTranslation extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$container->get('entity.repository')
);
}
/**
* EntityTranslation constructor.
*
* @param array $configuration
* The plugin configuration array.
* @param string $pluginId
* The plugin id.
* @param mixed $pluginDefinition
* The plugin definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* The entity repository service.
*
* @codeCoverageIgnore
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityRepositoryInterface $entityRepository) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->entityRepository = $entityRepository;
}
/**
* Resolver.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $language
* @param bool|null $access
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
* @param string|null $accessOperation
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return \Drupal\Core\Entity\EntityInterface|null
*/
public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
if ($entity instanceof TranslatableInterface && $entity->isTranslatable() && $entity->hasTranslation($language)) {
$entity = $entity->getTranslation($language);
$entity->addCacheContexts(["static:language:{$language}"]);
// Check if the passed user (or current user if none is passed) has access
// to the entity, if not return NULL.
if ($access) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access($accessOperation, $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return NULL;
}
}
return $entity;
}
return NULL;
}
}