You are here

protected function ResourceObjectNormalizer::getNormalization in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php \Drupal\jsonapi\Normalizer\ResourceObjectNormalizer::getNormalization()

Normalizes an entity using the given fieldset.

Parameters

string[] $field_names: The field names to normalize (the sparse fieldset, if any).

\Drupal\jsonapi\JsonApiResource\ResourceObject $object: The resource object to partially normalize.

string $format: The format in which the normalization will be encoded.

array $context: Context options for the normalizer.

Return value

array An array with two key-value pairs:

  • 'base': array, the base normalization of the entity, that does not depend on which sparse fieldset was requested.
  • 'fields': CacheableNormalization for all requested fields.

See also

::normalize()

1 call to ResourceObjectNormalizer::getNormalization()
ResourceObjectNormalizer::normalize in core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php, line 112

Class

ResourceObjectNormalizer
Converts the JSON:API module ResourceObject into a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function getNormalization(array $field_names, ResourceObject $object, $format = NULL, array $context = []) {
  $cached_normalization_parts = $this->cacher
    ->get($object);
  $normalizer_values = $cached_normalization_parts !== FALSE ? $cached_normalization_parts : static::buildEmptyNormalization($object);
  $fields =& $normalizer_values[ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_FIELDS];
  $non_cached_fields = array_diff_key($object
    ->getFields(), $fields);
  $non_cached_requested_fields = array_intersect_key($non_cached_fields, array_flip($field_names));
  foreach ($non_cached_requested_fields as $field_name => $field) {
    $fields[$field_name] = $this
      ->serializeField($field, $context, $format);
  }

  // Add links if missing.
  $base =& $normalizer_values[ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_BASE];
  $base['links'] = isset($base['links']) ? $base['links'] : $this->serializer
    ->normalize($object
    ->getLinks(), $format, $context)
    ->omitIfEmpty();
  if (!empty($non_cached_requested_fields)) {
    $this->cacher
      ->saveOnTerminate($object, $normalizer_values);
  }
  return $normalizer_values;
}