You are here

protected function EntityToJsonApi::calculateContext in JSON:API 8

Calculate the context for the serialize/normalize operation.

Parameters

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

Return value

array The context.

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

File

src/EntityToJsonApi.php, line 122

Class

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

Namespace

Drupal\jsonapi

Code

protected function calculateContext(EntityInterface $entity) {

  // TODO: Supporting includes requires adding the 'include' query string.
  $resource_type = $this->resourceTypeRepository
    ->get($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  $resource_path = $resource_type
    ->getPath();
  $path = sprintf('%s%s/%s', $this->jsonApiBasePath, $resource_path, $entity
    ->uuid());
  $master_request = $this->requestStack
    ->getMasterRequest();
  $request = Request::create($master_request
    ->getSchemeAndHttpHost() . $master_request
    ->getBaseUrl() . $path, 'GET');
  return [
    'account' => $this->currentUser,
    'resource_type' => $resource_type,
    'request' => $request,
  ];
}