function FieldItemNormalizer::doNormalize
This normalizer leaves JSON:API normalizer land and enters the land of Drupal core's serialization system. That system was never designed with cacheability in mind, and hence bubbles cacheability out of band. This must catch it, and pass it to the value object that JSON:API uses.
Overrides SchematicNormalizerTrait::doNormalize
File
-
core/
modules/ jsonapi/ src/ Normalizer/ FieldItemNormalizer.php, line 62
Class
- FieldItemNormalizer
- Converts the Drupal field item object to a JSON:API array structure.
Namespace
Drupal\jsonapi\NormalizerCode
public function doNormalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
assert($object instanceof FieldItemInterface);
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY] = new CacheableMetadata();
// Default: The field has only internal (or no) properties but has a public
// value.
$values = $object->getValue();
// There are non-internal properties. Normalize those.
if ($field_properties = TypedDataInternalPropertiesHelper::getNonInternalProperties($object)) {
// We normalize each individual value, so each can do their own casting,
// if needed.
$values = array_map(function ($property) use ($format, $context) {
return $this->serializer
->normalize($property, $format, $context);
}, $field_properties);
// Flatten if there is only a single property to normalize.
$flatten = count($field_properties) === 1 && $object::mainPropertyName() !== NULL;
$values = static::rasterizeValueRecursive($flatten ? reset($values) : $values);
}
$normalization = new CacheableNormalization($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY], $values);
unset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
return $normalization;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.