You are here

public function EntityReference::resolve in GraphQL 8.4

Resolve entity references in the given field name.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

string $field:

string|null $language:

array|null $bundles:

bool|null $access:

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

string|null $accessOperation:

\Drupal\graphql\GraphQL\Execution\FieldContext $context:

Return value

\GraphQL\Deferred|null

File

src/Plugin/GraphQL/DataProducer/Field/EntityReference.php, line 150

Class

EntityReference
Loads entities from an entity reference field.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Field

Code

public function resolve(EntityInterface $entity, $field, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
  if (!$entity instanceof FieldableEntityInterface || !$entity
    ->hasField($field)) {
    return NULL;
  }
  $definition = $entity
    ->getFieldDefinition($field);
  $type = $definition
    ->getSetting('target_type');
  $values = $entity
    ->get($field);
  if ($values instanceof EntityReferenceFieldItemListInterface) {
    $ids = array_map(function ($value) {
      return $value['target_id'];
    }, $values
      ->getValue());
    $resolver = $this->entityBuffer
      ->add($type, $ids);
    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;
}