function EntityResource::getEntityFromResourceIdentifier

Same name in other branches
  1. 9 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::getEntityFromResourceIdentifier()
  2. 8.9.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::getEntityFromResourceIdentifier()
  3. 10 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 963

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.