You are here

protected function ResourceObjectNormalizer::serializeField in JSON:API 8.2

Serializes a given field.

Parameters

mixed $field: The field to serialize.

array $context: The normalization context.

string $format: The serialization format.

Return value

\Drupal\jsonapi\Normalizer\Value\CacheableNormalization The normalized value.

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

File

src/Normalizer/ResourceObjectNormalizer.php, line 84

Class

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

Namespace

Drupal\jsonapi\Normalizer

Code

protected function serializeField($field, array $context, $format) {

  // Only content entities contain FieldItemListInterface fields. Since config
  // entities do not have "real" fields and therefore do not have field access
  // restrictions.
  if ($field instanceof FieldItemListInterface) {
    $field_access_result = $field
      ->access('view', $context['account'], TRUE);
    if (!$field_access_result
      ->isAllowed()) {
      return new CacheableOmission(CacheableMetadata::createFromObject($field_access_result));
    }
    $normalized_field = $this->serializer
      ->normalize($field, $format, $context);
    assert($normalized_field instanceof CacheableNormalization);
    return $normalized_field
      ->withCacheableDependency(CacheableMetadata::createFromObject($field_access_result));
  }
  else {

    // Config "fields" in this case are arrays or primitives and do not need
    // to be normalized.
    return CacheableNormalization::permanent($field);
  }
}