You are here

public function EntityTranslations::resolve in GraphQL 8.4

Resolver.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

bool|null $access:

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

string|null $accessOperation:

Return value

array|null

File

src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php, line 102

Class

EntityTranslations
Returns all available translations of an entity.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Entity

Code

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

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