-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathEntityTranslations.php
More file actions
130 lines (120 loc) · 4.14 KB
/
EntityTranslations.php
File metadata and controls
130 lines (120 loc) · 4.14 KB
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
126
127
128
129
130
<?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\Language\LanguageInterface;
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 all available translations of an entity.
*
* @DataProducer(
* id = "entity_translations",
* name = @Translation("Entity translations"),
* description = @Translation("Returns all available translations of an entity"),
* produces = @ContextDefinition("entity",
* label = @Translation("Translated entity"),
* multiple = TRUE,
* required = FALSE
* ),
* consumes = {
* "entity" = @ContextDefinition("entity",
* label = @Translation("Entity")
* ),
* "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 EntityTranslations 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 bool|null $access
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
* @param string|null $accessOperation
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return array|null
*/
public function resolve(EntityInterface $entity, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
$languages = $entity->getTranslationLanguages();
return array_map(function (LanguageInterface $language) use ($entity, $access, $accessOperation, $accessUser, $context) {
$langcode = $language->getId();
if (!$entity->hasTranslation($langcode)) {
return NULL;
}
$entity = $entity->getTranslation($langcode);
$entity->addCacheContexts(["static:language:{$langcode}"]);
if ($access) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access($accessOperation, $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return NULL;
}
}
return $entity;
}, $languages);
}
return NULL;
}
}