function ResourceObjectNormalizer::getNormalization

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php \Drupal\jsonapi\Normalizer\ResourceObjectNormalizer::getNormalization()
  2. 10 core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php \Drupal\jsonapi\Normalizer\ResourceObjectNormalizer::getNormalization()
  3. 11.x 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

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'] = $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.