|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
|
6
|
|
|
use Drupal\Core\Entity\ContentEntityInterface; |
|
7
|
|
|
use Drupal\Core\Entity\EntityRepositoryInterface; |
|
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
9
|
|
|
use Drupal\Core\TypedData\TranslatableInterface; |
|
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
12
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @GraphQLField( |
|
16
|
|
|
* id = "entity_translations", |
|
17
|
|
|
* name = "entityTranslations", |
|
18
|
|
|
* secure = true, |
|
19
|
|
|
* type = "[Entity]", |
|
20
|
|
|
* parents = {"Entity"} |
|
21
|
|
|
* ) |
|
22
|
|
|
*/ |
|
23
|
|
|
class EntityTranslations extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
24
|
|
|
use DependencySerializationTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The entity repository service. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Drupal\Core\Entity\EntityRepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $entityRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
37
|
|
|
return new static( |
|
38
|
|
|
$configuration, |
|
39
|
|
|
$pluginId, |
|
40
|
|
|
$pluginDefinition, |
|
41
|
|
|
$container->get('entity.repository') |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* EntityTranslations constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $configuration |
|
49
|
|
|
* The plugin configuration array. |
|
50
|
|
|
* @param string $pluginId |
|
51
|
|
|
* The plugin id. |
|
52
|
|
|
* @param mixed $pluginDefinition |
|
53
|
|
|
* The plugin definition array. |
|
54
|
|
|
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository |
|
55
|
|
|
* The entity repository service. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
array $configuration, |
|
59
|
|
|
$pluginId, |
|
60
|
|
|
$pluginDefinition, |
|
61
|
|
|
EntityRepositoryInterface $entityRepository |
|
62
|
|
|
) { |
|
63
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
64
|
|
|
$this->entityRepository = $entityRepository; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
|
71
|
|
|
if ($value instanceof ContentEntityInterface && $value instanceof TranslatableInterface && $value->isTranslatable()) { |
|
|
|
|
|
|
72
|
|
|
$languages = $value->getTranslationLanguages(); |
|
73
|
|
|
foreach ($languages as $language) { |
|
74
|
|
|
yield $this->entityRepository->getTranslationFromContext($value, $language->getId()); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|