You are here

public function TaxonomyLoadTree::resolve in GraphQL 8.4

Resolves the taxonomy tree for given vocabulary.

Parameters

string $vid: The vocanulary ID.

int $parent: The ID of the parent's term to load the tree for.

int|null $max_depth: Max depth to search in.

string|null $language: Optional. Language to be respected for retrieved entities.

bool $access: Whether check for access or not. Default is true.

\Drupal\Core\Session\AccountInterface|null $accessUser: User entity to check access for. Default is null.

string $accessOperation: Operation to check access for. Default is view.

\Drupal\graphql\GraphQL\Execution\FieldContext $context: The caching context related to the current field.

Return value

\GraphQL\Deferred|null A promise that will return entities or NULL if there aren't any.

File

src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php, line 148

Class

TaxonomyLoadTree
Loads the taxonomy tree.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Taxonomy

Code

public function resolve(string $vid, int $parent, ?int $max_depth, ?string $language, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context) : ?Deferred {
  if (!isset($max_depth)) {
    $max_depth = self::MAX_DEPTH;
  }
  $terms = $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadTree($vid, $parent, $max_depth);
  $term_ids = array_column($terms, 'tid');
  $resolver = $this->entityBuffer
    ->add('taxonomy_term', $term_ids);
  return new Deferred(function () use ($language, $resolver, $context, $access, $accessUser, $accessOperation) {

    /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
    $entities = $resolver();
    if (!$entities) {

      // If there is no entity with this id, add the list cache tags so that
      // the cache entry is purged whenever a new entity of this type is
      // saved.
      $type = $this->entityTypeManager
        ->getDefinition('taxonomy_term');

      /** @var \Drupal\Core\Entity\EntityTypeInterface $type */
      $tags = $type
        ->getListCacheTags();
      $context
        ->addCacheTags($tags);
      return [];
    }
    foreach ($entities as $id => $entity) {
      $context
        ->addCacheableDependency($entities[$id]);
      if (isset($language) && $language !== $entities[$id]
        ->language()
        ->getId() && $entities[$id] instanceof TranslatableInterface) {
        $entities[$id] = $entities[$id]
          ->getTranslation($language);
        $entities[$id]
          ->addCacheContexts([
          "static:language:{$language}",
        ]);
      }
      if ($access) {

        /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
        $accessResult = $entity
          ->access($accessOperation, $accessUser, TRUE);
        $context
          ->addCacheableDependency($accessResult);
        if (!$accessResult
          ->isAllowed()) {
          unset($entities[$id]);
          continue;
        }
      }
    }
    return $entities;
  });
}