Skip to content

Commit 0666a1f

Browse files
committed
fix(dataproducer): Fix entity label handling (by mxr576)
1 parent f12daab commit 0666a1f

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/Plugin/GraphQL/DataProducer/Entity/EntityLabel.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Entity;
44

55
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Session\AccountInterface;
7+
use Drupal\graphql\GraphQL\Execution\FieldContext;
68
use Drupal\graphql\Plugin\DataProducerPluginCachingInterface;
79
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
810

@@ -19,7 +21,12 @@
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
}

tests/src/Kernel/DataProducer/EntityTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\Tests\graphql\Kernel\DataProducer;
44

5+
use Drupal\Core\Access\AccessResult;
56
use Drupal\Core\Entity\EntityInterface;
67
use Drupal\Core\Language\LanguageInterface;
78
use Drupal\Core\Url;
@@ -199,9 +200,32 @@ public function testResolveLabel(): void {
199200
->method('label')
200201
->willReturn('Dummy label');
201202

203+
$this->entity->expects($this->exactly(2))
204+
->method('access')
205+
->willReturnCallback(static function (): AccessResult {
206+
static $counter = 0;
207+
switch ($counter) {
208+
case 0:
209+
$counter++;
210+
return AccessResult::allowed();
211+
212+
case 1:
213+
$counter++;
214+
return AccessResult::forbidden();
215+
216+
default:
217+
throw new \LogicException('The access() method should not have been called more than twice.');
218+
}
219+
})
220+
->with('view label', NULL, TRUE);
221+
202222
$this->assertEquals('Dummy label', $this->executeDataProducer('entity_label', [
203223
'entity' => $this->entity,
204224
]));
225+
226+
$this->assertNull($this->executeDataProducer('entity_label', [
227+
'entity' => $this->entity,
228+
]));
205229
}
206230

207231
/**

0 commit comments

Comments
 (0)