You are here

protected function EntityToJsonApi::calculateContext in JSON:API Extras 8.2

Calculate the arguments for the serialize/normalize operation.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to generate the JSON from.

string[] $includes: The list of includes.

Return value

array The list of arguments for serialize/normalize operation.

1 call to EntityToJsonApi::calculateContext()
EntityToJsonApi::serialize in src/EntityToJsonApi.php
Return the requested entity as a raw string.

File

src/EntityToJsonApi.php, line 141

Class

EntityToJsonApi
Simplifies the process of generating a JSON API version of an entity.

Namespace

Drupal\jsonapi_extras

Code

protected function calculateContext(EntityInterface $entity, array $includes = []) {
  $entity_type_id = $entity
    ->getEntityTypeId();
  $resource_type = $this->resourceTypeRepository
    ->get($entity_type_id, $entity
    ->bundle());

  // The overridden resource type implementation of "jsonapi_extras" may
  // return a value containing a leading slash. Since this was initial
  // behavior we won't going to break the things and ready to tackle both
  // cases: with or without a leading slash.
  $resource_path = ltrim($resource_type
    ->getPath(), '/');
  $path = sprintf('%s/%s/%s', rtrim($this->jsonApiBasePath, '/'), rtrim($resource_path, '/'), $entity
    ->uuid());
  $request = Request::create($this->masterRequest
    ->getUriForPath($path));

  // We don't have to filter the "$include" since this will be done later.
  // @see JsonApiDocumentTopLevelNormalizer::expandContext()
  $request->query
    ->set('include', implode(',', $includes));
  $request->attributes
    ->set($entity_type_id, $entity);
  $request->attributes
    ->set(Routes::RESOURCE_TYPE_KEY, $resource_type);
  $request->attributes
    ->set(Routes::JSON_API_ROUTE_FLAG_KEY, TRUE);
  return [
    'account' => $this->currentUser,
    'resource_type' => $resource_type,
    'request' => $request,
  ];
}