public function EntityResource::getRelationship in JSON:API 8.2
Same name and namespace in other branches
- 8 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 src/
Controller/ EntityResource.php - Adds a relationship to a to-many relationship.
- EntityResource::removeFromRelationshipData in src/
Controller/ EntityResource.php - Deletes the relationship of an entity.
- EntityResource::replaceRelationshipData in src/
Controller/ EntityResource.php - Updates the relationship of an entity.
File
- src/
Controller/ EntityResource.php, line 545
Class
- EntityResource
- Process all entity requests.
Namespace
Drupal\jsonapi\ControllerCode
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 RelationshipFieldAccess
// service, so we don't need to call ::getAccessCheckedResourceObject().
$resource_object = ResourceObject::createFromEntity($resource_type, $entity);
$relationship_object_urls = EntityReferenceFieldNormalizer::getRelationshipLinks($resource_object, $related);
$response = $this
->buildWrappedResponse($field_list, $request, $this
->getIncludes($request, $resource_object), $response_code, [], array_reduce(array_keys($relationship_object_urls), function (LinkCollection $links, $key) use ($relationship_object_urls) {
return $links
->withLink($key, new Link(new CacheableMetadata(), $relationship_object_urls[$key], [
$key,
]));
}, new LinkCollection([])));
// Add the host entity as a cacheable dependency.
$response
->addCacheableDependency($entity);
return $response;
}