Skip to content

Commit

Permalink
feat(dataproducer): make access check optional in entity_label (drupa…
Browse files Browse the repository at this point in the history
  • Loading branch information
edurenye committed Aug 26, 2024
1 parent a05b635 commit c59b1c5
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Plugin/GraphQL/DataProducer/Entity/EntityLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
* "entity" = @ContextDefinition("entity",
* label = @Translation("Entity")
* ),
* "access" = @ContextDefinition("boolean",
* label = @Translation("Check access"),
* required = FALSE,
* default_value = TRUE
* ),
* "access_user" = @ContextDefinition("entity:user",
* label = @Translation("User"),
* required = FALSE,
Expand All @@ -36,19 +41,22 @@ class EntityLabel extends DataProducerPluginBase implements DataProducerPluginCa
* Resolver.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param bool|null $access
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return string|null
*/
public function resolve(EntityInterface $entity, ?AccountInterface $accessUser, FieldContext $context) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access('view label', $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if ($accessResult->isAllowed()) {
return $entity->label();
public function resolve(EntityInterface $entity, ?bool $access, ?AccountInterface $accessUser, FieldContext $context) {
if ($access) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access('view label', $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return NULL;
}
}
return NULL;
return $entity->label();
}

}

0 comments on commit c59b1c5

Please sign in to comment.