1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
7
|
|
|
use Drupal\Core\Language\Language; |
8
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
9
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
10
|
|
|
use Drupal\Core\TypedData\TranslatableInterface; |
11
|
|
|
use Drupal\Core\Url; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Retrieve the current routes entity, if it is an entity route. |
18
|
|
|
* |
19
|
|
|
* TODO: Move this to `InternalUrl` (breaking change). |
20
|
|
|
* |
21
|
|
|
* @GraphQLField( |
22
|
|
|
* id = "route_entity", |
23
|
|
|
* secure = true, |
24
|
|
|
* name = "entity", |
25
|
|
|
* description = @Translation("The entity belonging to the current url."), |
26
|
|
|
* parents = {"EntityCanonicalUrl"}, |
27
|
|
|
* type = "Entity" |
28
|
|
|
* ) |
29
|
|
|
*/ |
30
|
|
|
class RouteEntity extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
31
|
|
|
use DependencySerializationTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The entity type manager. |
35
|
|
|
* |
36
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $entityTypeManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The language manager. |
42
|
|
|
* |
43
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
44
|
|
|
*/ |
45
|
|
|
protected $languageManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
array $configuration, |
52
|
|
|
$pluginId, |
53
|
|
|
$pluginDefinition, |
54
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
55
|
|
|
LanguageManagerInterface $languageManager |
56
|
|
|
) { |
57
|
|
|
$this->entityTypeManager = $entityTypeManager; |
58
|
|
|
$this->languageManager = $languageManager; |
59
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
66
|
|
|
return new static( |
67
|
|
|
$configuration, |
68
|
|
|
$pluginId, |
69
|
|
|
$pluginDefinition, |
70
|
|
|
$container->get('entity_type.manager'), |
71
|
|
|
$container->get('language_manager') |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
79
|
|
|
if ($value instanceof Url) { |
|
|
|
|
80
|
|
|
list(, $entityType) = explode('.', $value->getRouteName()); |
81
|
|
|
$parameters = $value->getRouteParameters(); |
82
|
|
|
$storage = $this->entityTypeManager->getStorage($entityType); |
83
|
|
|
$entity = $storage->load($parameters[$entityType]); |
84
|
|
|
$language = $this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT)->getId(); |
85
|
|
|
|
86
|
|
|
// Attempt to translate the entity. |
87
|
|
|
if ($entity instanceof TranslatableInterface && $entity->hasTranslation($language)) { |
|
|
|
|
88
|
|
|
$entity = $entity->getTranslation($language); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$access = $entity->access('view', NULL, TRUE); |
92
|
|
|
$this->commitCacheableDependency($info, $access); |
93
|
|
|
yield $access->isAllowed() ? $entity : NULL; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|