function FieldNormalizer::doNormalize

Normalizes an object into a set of arrays/scalars.

Parameters

mixed $object: Object to normalize.

string|null $format: Format the normalization result will be encoded as.

array $context: Context options for the normalizer.

Return value

array|string|int|float|bool|\ArrayObject|null The normalization. An \ArrayObject is used to make sure an empty object is encoded as an object not an array.

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.