You are here

protected function EntityResource::getEntityFromResourceIdentifier in Drupal 9

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

Loads the entity targeted by a resource identifier.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceIdentifier $resource_identifier: A resource identifier.

Return value

\Drupal\Core\Entity\EntityInterface The entity targeted by a resource identifier.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown if the given resource identifier targets a resource type or resource which does not exist.

File

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

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function getEntityFromResourceIdentifier(ResourceIdentifier $resource_identifier) {
  $resource_type_name = $resource_identifier
    ->getTypeName();
  if (!($target_resource_type = $this->resourceTypeRepository
    ->getByTypeName($resource_type_name))) {
    throw new BadRequestHttpException("The resource type `{$resource_type_name}` does not exist.");
  }
  $id = $resource_identifier
    ->getId();
  if (!($targeted_resource = $this->entityRepository
    ->loadEntityByUuid($target_resource_type
    ->getEntityTypeId(), $id))) {
    throw new BadRequestHttpException("The targeted `{$resource_type_name}` resource with ID `{$id}` does not exist.");
  }
  return $targeted_resource;
}