You are here

public function FieldNormalizer::normalize in Drupal 8

Same name in this branch
  1. 8 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
  2. 8 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer::normalize()
Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
1 method overrides FieldNormalizer::normalize()
EntityReferenceFieldNormalizer::normalize in core/modules/jsonapi/src/Normalizer/EntityReferenceFieldNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

core/modules/jsonapi/src/Normalizer/FieldNormalizer.php, line 33

Class

FieldNormalizer
Converts the Drupal field structure to a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

public function normalize($field, $format = NULL, array $context = []) {

  /* @var \Drupal\Core\Field\FieldItemListInterface $field */
  $normalized_items = $this
    ->normalizeFieldItems($field, $format, $context);
  assert($context['resource_object'] instanceof ResourceObject);
  return $context['resource_object']
    ->getResourceType()
    ->getFieldByInternalName($field
    ->getName())
    ->hasOne() ? array_shift($normalized_items) ?: CacheableNormalization::permanent(NULL) : CacheableNormalization::aggregate($normalized_items);
}