You are here

public function EntityResource::getRelationship in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::getRelationship()

Gets the relationship of an entity.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The base JSON:API resource type for the request to be served.

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

string $related: The related field name.

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

int $response_code: The response code. Defaults to 200.

Return value

\Drupal\jsonapi\ResourceResponse The response.

3 calls to EntityResource::getRelationship()
EntityResource::addToRelationshipData in core/modules/jsonapi/src/Controller/EntityResource.php
Adds a relationship to a to-many relationship.
EntityResource::removeFromRelationshipData in core/modules/jsonapi/src/Controller/EntityResource.php
Deletes the relationship of an entity.
EntityResource::replaceRelationshipData in core/modules/jsonapi/src/Controller/EntityResource.php
Updates the relationship of an entity.

File

core/modules/jsonapi/src/Controller/EntityResource.php, line 564

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

public function getRelationship(ResourceType $resource_type, FieldableEntityInterface $entity, $related, Request $request, $response_code = 200) {

  /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
  $field_list = $entity
    ->get($resource_type
    ->getInternalName($related));

  // Access will have already been checked by the RelationshipRouteAccessCheck
  // service, so we don't need to call ::getAccessCheckedResourceObject().
  $resource_object = ResourceObject::createFromEntity($resource_type, $entity);
  $relationship = Relationship::createFromEntityReferenceField($resource_object, $field_list);
  $response = $this
    ->buildWrappedResponse($relationship, $request, $this
    ->getIncludes($request, $resource_object), $response_code);

  // Add the host entity as a cacheable dependency.
  if ($response instanceof CacheableResponseInterface) {
    $response
      ->addCacheableDependency($entity);
  }
  return $response;
}