You are here

public function EntityToJsonApi::normalize in JSON:API Extras 8.2

Same name and namespace in other branches
  1. 8.3 src/EntityToJsonApi.php \Drupal\jsonapi_extras\EntityToJsonApi::normalize()

Return the requested entity as an structured array.

Parameters

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

string[] $includes: The list of includes.

Return value

array The JSON structure of the requested resource.

File

src/EntityToJsonApi.php, line 118

Class

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

Namespace

Drupal\jsonapi_extras

Code

public function normalize(EntityInterface $entity, array $includes = []) {
  $referenced_entities = [];
  foreach ($includes as $field_name) {
    $referenced_entities = array_merge($referenced_entities, $entity
      ->get($field_name)
      ->referencedEntities());
  }
  $document = new JsonApiDocumentTopLevel($entity);
  return $this->serializer
    ->normalize($document, 'api_json', $this
    ->calculateContext($entity, $includes))
    ->rasterizeValue();
}