public function RelationshipFieldDefinitionNormalizer::normalize in Schemata 8
Overrides ListDataDefinitionNormalizer::normalize
File
- schemata_json_schema/
src/ Normalizer/ jsonapi/ RelationshipFieldDefinitionNormalizer.php, line 60
Class
- RelationshipFieldDefinitionNormalizer
- Normalizer for RelationshipFieldDefinitionNormalizer objects.
Namespace
Drupal\schemata_json_schema\Normalizer\jsonapiCode
public function normalize($entity, $format = NULL, array $context = []) {
$cardinality = $entity
->getFieldStorageDefinition()
->getCardinality();
$context['cardinality'] = $cardinality;
/* @var $entity \Drupal\Core\Field\FieldDefinitionInterface */
$normalized = [
'properties' => [
'relationships' => [
'description' => t('Entity relationships'),
'properties' => [
$context['name'] => $this
->normalizeRelationship($entity),
],
'type' => 'object',
],
],
];
// Specify non-contextual default value as an example.
$default_value = $entity
->getDefaultValueLiteral();
if (!empty($default_value)) {
$normalized['properties']['relationships']['properties'][$context['name']]['default'] = $default_value;
}
// The cardinality is the configured maximum number of values the field can
// contain. If unlimited, we do not include a maxItems attribute.
if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && $cardinality != 1) {
$normalized['properties']['relationships']['properties'][$context['name']]['maxItems'] = $cardinality;
}
return $normalized;
}