You are here

public function EntityResource::getRelated in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::getRelated()

Gets the related resource.

Parameters

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

string $related_field: The related field name.

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

\Drupal\jsonapi\ResourceResponse The response.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

File

src/Controller/EntityResource.php, line 467

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

public function getRelated(EntityInterface $entity, $related_field, Request $request) {
  $related_field = $this->resourceType
    ->getInternalName($related_field);
  $this
    ->relationshipAccess($entity, 'view', $related_field);

  /* @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
  $field_list = $entity
    ->get($related_field);
  $this
    ->validateReferencedResource($field_list, $related_field);

  // Add the cacheable metadata from the host entity.
  $cacheable_metadata = CacheableMetadata::createFromObject($entity);
  $is_multiple = $field_list
    ->getDataDefinition()
    ->getFieldStorageDefinition()
    ->isMultiple();
  if (!$is_multiple && $field_list->entity) {
    $response = $this
      ->getIndividual($field_list->entity, $request);

    // Add cacheable metadata for host entity to individual response.
    $response
      ->addCacheableDependency($cacheable_metadata);
    return $response;
  }
  $collection_data = [];

  // Remove the entities pointing to a resource that may be disabled. Even
  // though the normalizer skips disabled references, we can avoid unnecessary
  // work by checking here too.

  /* @var \Drupal\Core\Entity\EntityInterface[] $referenced_entities */
  $referenced_entities = array_filter($field_list
    ->referencedEntities(), function (EntityInterface $entity) {
    return (bool) $this->resourceTypeRepository
      ->get($entity
      ->getEntityTypeId(), $entity
      ->bundle());
  });
  foreach ($referenced_entities as $referenced_entity) {
    $collection_data[$referenced_entity
      ->id()] = static::getEntityAndAccess($referenced_entity);
    $cacheable_metadata
      ->addCacheableDependency($referenced_entity);
  }
  $entity_collection = new EntityCollection(array_column($collection_data, 'entity'));
  $response = $this
    ->buildWrappedResponse($entity_collection);
  $access_info = array_column($collection_data, 'access');
  array_walk($access_info, function ($access) use ($response) {
    $response
      ->addCacheableDependency($access);
  });

  // $response does not contain the entity list cache tag. We add the
  // cacheable metadata for the finite list of entities in the relationship.
  $response
    ->addCacheableDependency($cacheable_metadata);
  return $response;
}