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