function RelationshipNormalizer::getNormalizationSchema
Overrides SchematicNormalizerTrait::getNormalizationSchema
File
-
core/
modules/ jsonapi/ src/ Normalizer/ RelationshipNormalizer.php, line 46
Class
- RelationshipNormalizer
- Normalizes a JSON:API relationship object.
Namespace
Drupal\jsonapi\NormalizerCode
public function getNormalizationSchema(mixed $object, array $context = []) : array {
assert($object instanceof Relationship);
$schema = [
'allOf' => [
[
'$ref' => JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#/definitions/relationship',
],
],
];
$field_definition = $object->getContext()
->getField($object->getFieldName())?->getFieldDefinition();
$item_class = $field_definition?->getItemDefinition()->getClass();
assert($item_class, sprintf('The context ResourceObject for Relationship being normalized is missing field %s.', $object->getFieldName()));
if (!$item_class || !is_subclass_of($item_class, EntityReferenceItemInterface::class)) {
return $schema;
}
$targets = $item_class::getReferenceableBundles($field_definition);
$target_types = array_reduce(array_keys($targets), function (array $carry, string $entity_type_id) use ($targets) {
foreach ($targets[$entity_type_id] as $bundle) {
// Even if a resource is internal, it can be referenced.
if (!($resource = $this->resourceTypeRepository
->get($entity_type_id, $bundle)) || in_array($resource->getTypeName(), $carry)) {
continue;
}
$carry[] = $resource->getTypeName();
}
return $carry;
}, []);
if ($target_types) {
$schema['properties']['type'] = [
'oneOf' => array_map(fn(string $resource_type_name) => [
'const' => $resource_type_name,
], $target_types),
];
}
return $schema;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.