You are here

public function EntityReferenceRevisions::resolve in GraphQL 8.4

Resolves entity reference revisions for a given field of a given entity.

May optionally respect the entity bundles and language.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

string $field: The field of a given entity to get entity reference revisions for.

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

array|null $bundles: Optional. List of bundles 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/Field/EntityReferenceRevisions.php, line 150

Class

EntityReferenceRevisions
Loads the entity reference revisions.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Field

Code

public function resolve(EntityInterface $entity, string $field, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context) : ?Deferred {
  if (!$entity instanceof FieldableEntityInterface || !$entity
    ->hasField($field)) {
    return NULL;
  }
  $definition = $entity
    ->getFieldDefinition($field);
  if ($definition
    ->getType() !== 'entity_reference_revisions') {
    return NULL;
  }
  $definition = $entity
    ->getFieldDefinition($field);
  $type = $definition
    ->getSetting('target_type');
  $values = $entity
    ->get($field);
  if ($values instanceof EntityReferenceFieldItemListInterface) {
    $vids = array_map(function ($value) {
      return $value['target_revision_id'];
    }, $values
      ->getValue());
    $resolver = $this->entityRevisionBuffer
      ->add($type, $vids);
    return new Deferred(function () use ($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context) {
      return $this
        ->getReferencedEntities($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context);
    });
  }
  return NULL;
}