function FieldNormalizer::getNormalizationSchema
Overrides SchematicNormalizerTrait::getNormalizationSchema
File
-
core/
modules/ jsonapi/ src/ Normalizer/ FieldNormalizer.php, line 100
Class
- FieldNormalizer
- Converts the Drupal field structure to a JSON:API array structure.
Namespace
Drupal\jsonapi\NormalizerCode
protected function getNormalizationSchema(mixed $object, array $context = []) : array {
assert($object instanceof FieldItemListInterface);
// Some aspects of the schema are determined by the field config.
$cardinality = $object->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
$schema = [];
// Normalizers are resolved by the class/object being normalized. Even
// without data, we must retrieve a representative field item.
$field_item = $object->appendItem($object->generateSampleItems());
assert($this->serializer instanceof JsonSchemaProviderSerializerInterface);
$item_schema = $this->serializer
->getJsonSchema($field_item, $context);
$object->removeItem(count($object) - 1);
unset($field_item);
$schema = $item_schema;
if ($cardinality !== 1) {
$schema['type'] = 'array';
if ($object->getFieldDefinition()
->isRequired()) {
$schema['minItems'] = 1;
}
if ($cardinality !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$schema['maxItems'] = $cardinality;
}
if (!empty($item_schema)) {
$schema['items'] = $item_schema;
}
}
return !$object->getFieldDefinition()
->isRequired() ? [
'oneOf' => [
$schema,
[
'type' => 'null',
],
],
] : $schema;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.