1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
6
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
7
|
|
|
use Drupal\Core\Entity\EntityInterface; |
8
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
9
|
|
|
use Drupal\Core\Language\Language; |
10
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
11
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
12
|
|
|
use Drupal\Core\TypedData\TranslatableInterface; |
13
|
|
|
use Drupal\Core\Url; |
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Retrieve the current routes entity, if it is an entity route. |
20
|
|
|
* |
21
|
|
|
* TODO: Move this to `InternalUrl` (breaking change). |
22
|
|
|
* |
23
|
|
|
* @GraphQLField( |
24
|
|
|
* id = "route_entity", |
25
|
|
|
* secure = true, |
26
|
|
|
* name = "entity", |
27
|
|
|
* description = @Translation("The entity belonging to the current url."), |
28
|
|
|
* parents = {"EntityCanonicalUrl"}, |
29
|
|
|
* type = "Entity" |
30
|
|
|
* ) |
31
|
|
|
*/ |
32
|
|
|
class RouteEntity extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
33
|
|
|
use DependencySerializationTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The entity type manager. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $entityTypeManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The language manager. |
44
|
|
|
* |
45
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
46
|
|
|
*/ |
47
|
|
|
protected $languageManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
array $configuration, |
54
|
|
|
$pluginId, |
55
|
|
|
$pluginDefinition, |
56
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
57
|
|
|
LanguageManagerInterface $languageManager |
58
|
|
|
) { |
59
|
|
|
$this->entityTypeManager = $entityTypeManager; |
60
|
|
|
$this->languageManager = $languageManager; |
61
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
68
|
|
|
return new static( |
69
|
|
|
$configuration, |
70
|
|
|
$pluginId, |
71
|
|
|
$pluginDefinition, |
72
|
|
|
$container->get('entity_type.manager'), |
73
|
|
|
$container->get('language_manager') |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
81
|
|
|
if ($value instanceof Url) { |
|
|
|
|
82
|
|
|
list($prefix, $entityType, $suffix) = explode('.', $value->getRouteName()); |
83
|
|
|
$parameters = $value->getRouteParameters(); |
84
|
|
|
|
85
|
|
|
if (!($prefix === 'entity' && $suffix === 'canonical') || !array_key_exists($entityType, $parameters)) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$entity = $this->entityTypeManager |
90
|
|
|
->getStorage($entityType) |
91
|
|
|
->load($parameters[$entityType]); |
92
|
|
|
|
93
|
|
|
$lang = $this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT)->getId(); |
94
|
|
|
|
95
|
|
|
if ($entity instanceof TranslatableInterface && $entity->hasTranslation($lang)) { |
|
|
|
|
96
|
|
|
$translation = $entity->getTranslation($lang); |
97
|
|
|
if ($translation->access('view')) { |
98
|
|
|
yield $translation; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
else if ($entity->access('view')) { |
102
|
|
|
yield $entity; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Intermediate solution. Shouldn't the entity (stored in result) have it's |
110
|
|
|
* cache tag? Right now thats not the case. |
111
|
|
|
*/ |
112
|
|
|
protected function getCacheDependencies($result, $parent, array $args) { |
113
|
|
|
$dependencies = parent::getCacheDependencies($result, $parent, $args); |
114
|
|
|
foreach ($result as $item) { |
115
|
|
|
if ($item instanceof EntityInterface) { |
|
|
|
|
116
|
|
|
$metadata = new CacheableMetadata(); |
117
|
|
|
$metadata->addCacheTags(['node:' . $item->id()]); |
118
|
|
|
$dependencies[] = $metadata; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
return $dependencies; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|