You are here

public function EntityTranslation::resolve in GraphQL 8.4

Resolver.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

string $language:

bool|null $access:

\Drupal\Core\Session\AccountInterface|null $accessUser:

string|null $accessOperation:

Return value

\Drupal\Core\Entity\EntityInterface|null

File

src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php, line 103

Class

EntityTranslation
Returns the entity in a given language.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Entity

Code

public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation) {
  if ($entity instanceof TranslatableInterface && $entity
    ->isTranslatable()) {
    $entity = $entity
      ->getTranslation($language);
    $entity
      ->addCacheContexts([
      "static:language:{$language}",
    ]);

    // Check if the passed user (or current user if none is passed) has access
    // to the entity, if not return NULL.
    if ($access) {

      /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
      $accessResult = $entity
        ->access($accessOperation, $accessUser, TRUE);
      if (!$accessResult
        ->isAllowed()) {
        return NULL;
      }
    }
    return $entity;
  }
  return NULL;
}