function FieldNormalizer::doNormalize

Overrides SchematicNormalizerTrait::doNormalize

File

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

Class

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

Namespace

Drupal\jsonapi\Normalizer

Code

public function doNormalize($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.