function FieldNormalizer::normalize

Same name in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
  2. 9 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer::normalize()
  3. 8.9.x core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
  4. 8.9.x core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer::normalize()
  5. 10 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

File

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

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 = []) : array|string|int|float|bool|\ArrayObject|null {
    
    /** @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);
}

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