You are here

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

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

Return the requested entity as a raw string.

Parameters

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

string[] $includes: The list of includes.

Return value

string The raw JSON string of the requested resource.

File

src/EntityToJsonApi.php, line 95

Class

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

Namespace

Drupal\jsonapi_extras

Code

public function serialize(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
    ->serialize($document, 'api_json', $this
    ->calculateContext($entity, $includes));
}