2
2
3
3
namespace Drupal \graphql \Plugin \GraphQL \DataProducer \Entity ;
4
4
5
+ use Drupal \Core \Access \AccessManagerInterface ;
6
+ use Drupal \Core \DependencyInjection \DependencySerializationTrait ;
5
7
use Drupal \Core \Entity \EntityInterface ;
8
+ use Drupal \Core \Plugin \ContainerFactoryPluginInterface ;
9
+ use Drupal \Core \Session \AccountInterface ;
10
+ use Drupal \graphql \GraphQL \Execution \FieldContext ;
6
11
use Drupal \graphql \Plugin \GraphQL \DataProducer \DataProducerPluginBase ;
12
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
7
13
8
14
/**
9
15
* Returns the URL of an entity.
28
34
* label = @Translation("URL Options"),
29
35
* description = @Translation("Options to pass to the toUrl call"),
30
36
* required = FALSE
31
- * )
37
+ * ),
38
+ * "access_user" = @ContextDefinition("entity:user",
39
+ * label = @Translation("User"),
40
+ * required = FALSE,
41
+ * default_value = NULL
42
+ * ),
32
43
* }
33
44
* )
34
45
*/
35
- class EntityUrl extends DataProducerPluginBase {
46
+ class EntityUrl extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
47
+ use DependencySerializationTrait;
48
+
49
+ /**
50
+ * The access manager.
51
+ *
52
+ * @var \Drupal\Core\Access\AccessManagerInterface
53
+ */
54
+ protected $ accessManager ;
55
+
56
+ /**
57
+ * {@inheritdoc}
58
+ *
59
+ * @codeCoverageIgnore
60
+ */
61
+ public static function create (ContainerInterface $ container , array $ configuration , $ pluginId , $ pluginDefinition ) {
62
+ return new static (
63
+ $ configuration ,
64
+ $ pluginId ,
65
+ $ pluginDefinition ,
66
+ $ container ->get ('access_manager ' )
67
+ );
68
+ }
69
+
70
+ /**
71
+ * EntityTranslation constructor.
72
+ *
73
+ * @param array $configuration
74
+ * The plugin configuration array.
75
+ * @param string $pluginId
76
+ * The plugin id.
77
+ * @param mixed $pluginDefinition
78
+ * The plugin definition.
79
+ * @param \Drupal\Core\Access\AccessManagerInterface $accessManager
80
+ * The access manager service.
81
+ *
82
+ * @codeCoverageIgnore
83
+ */
84
+ public function __construct (array $ configuration , $ pluginId , $ pluginDefinition , AccessManagerInterface $ accessManager ) {
85
+ parent ::__construct ($ configuration , $ pluginId , $ pluginDefinition );
86
+ $ this ->accessManager = $ accessManager ;
87
+ }
36
88
37
89
/**
38
90
* Resolver.
@@ -43,13 +95,24 @@ class EntityUrl extends DataProducerPluginBase {
43
95
* The link relationship type, for example: canonical or edit-form.
44
96
* @param array|null $options
45
97
* The options to provided to the URL generator.
98
+ * @param \Drupal\Core\Session\AccountInterface|null $accessUser
99
+ * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
46
100
*
47
- * @return \Drupal\Core\Url
101
+ * @return \Drupal\Core\Url|null
48
102
*
49
103
* @throws \Drupal\Core\Entity\EntityMalformedException
50
104
*/
51
- public function resolve (EntityInterface $ entity , ?string $ rel , ?array $ options ) {
52
- return $ entity ->toUrl ($ rel ?? 'canonical ' , $ options ?? []);
105
+ public function resolve (EntityInterface $ entity , ?string $ rel , ?array $ options , ?AccountInterface $ accessUser , FieldContext $ context ) {
106
+ $ url = $ entity ->toUrl ($ rel ?? 'canonical ' , $ options ?? []);
107
+
108
+ // @see https://www.drupal.org/project/drupal/issues/2677902
109
+ $ access = $ this ->accessManager ->checkNamedRoute ($ url ->getRouteName (), $ url ->getRouteParameters (), $ accessUser , TRUE );
110
+ $ context ->addCacheableDependency ($ access );
111
+ if ($ access ->isAllowed ()) {
112
+ return $ url ;
113
+ }
114
+
115
+ return NULL ;
53
116
}
54
117
55
118
}
0 commit comments