33namespace Drupal \graphql \Plugin \GraphQL \DataProducer \Entity ;
44
55use Drupal \Core \Entity \EntityInterface ;
6+ use Drupal \Core \Session \AccountInterface ;
7+ use Drupal \graphql \GraphQL \Execution \FieldContext ;
68use Drupal \graphql \Plugin \DataProducerPluginCachingInterface ;
79use Drupal \graphql \Plugin \GraphQL \DataProducer \DataProducerPluginBase ;
810
1921 * consumes = {
2022 * "entity" = @ContextDefinition("entity",
2123 * label = @Translation("Entity")
22- * )
24+ * ),
25+ * "access_user" = @ContextDefinition("entity:user",
26+ * label = @Translation("User"),
27+ * required = FALSE,
28+ * default_value = NULL
29+ * ),
2330 * }
2431 * )
2532 */
@@ -29,11 +36,19 @@ class EntityLabel extends DataProducerPluginBase implements DataProducerPluginCa
2936 * Resolver.
3037 *
3138 * @param \Drupal\Core\Entity\EntityInterface $entity
39+ * @param \Drupal\Core\Session\AccountInterface|null $accessUser
40+ * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
3241 *
3342 * @return string|null
3443 */
35- public function resolve (EntityInterface $ entity ) {
36- return $ entity ->label ();
44+ public function resolve (EntityInterface $ entity , ?AccountInterface $ accessUser , FieldContext $ context ) {
45+ /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
46+ $ accessResult = $ entity ->access ('view label ' , $ accessUser , TRUE );
47+ $ context ->addCacheableDependency ($ accessResult );
48+ if ($ accessResult ->isAllowed ()) {
49+ return $ entity ->label ();
50+ }
51+ return NULL ;
3752 }
3853
3954}
0 commit comments