protected static function JsonApiSchemaController::addRelationshipsSchemaLinks in JSON:API Schema 8
1 call to JsonApiSchemaController::addRelationshipsSchemaLinks()
- JsonApiSchemaController::getResourceObjectSchema in src/
Controller/ JsonApiSchemaController.php
File
- src/
Controller/ JsonApiSchemaController.php, line 250
Class
Namespace
Drupal\jsonapi_schema\ControllerCode
protected static function addRelationshipsSchemaLinks(array $schema, ResourceType $resource_type, CacheableMetadata $cacheability) {
$resource_relationships = array_filter($resource_type
->getFields(), function (ResourceTypeField $field) {
return $field
->isFieldEnabled() && $field instanceof ResourceTypeRelationship;
});
if (empty($resource_relationships)) {
return $schema;
}
$schema['allOf'][0]['properties']['relationships'] = [
'$ref' => '#/definitions/relationships',
];
$relationships = array_reduce($resource_relationships, function ($relationships, ResourceTypeRelationship $relationship) use ($resource_type, $cacheability) {
if ($resource_type
->isInternal() || !Routes::hasNonInternalTargetResourceTypes($relationship
->getRelatableResourceTypes())) {
return $relationships;
}
$field_name = $relationship
->getPublicName();
$resource_type_name = $resource_type
->getTypeName();
$related_route_name = "jsonapi_schema.{$resource_type_name}.{$field_name}.related";
$related_schema_uri = Url::fromRoute($related_route_name)
->setAbsolute()
->toString(TRUE);
$cacheability
->addCacheableDependency($related_schema_uri);
return NestedArray::mergeDeep($relationships, [
$field_name => [
'links' => [
[
'href' => '{instanceHref}',
'rel' => 'related',
'targetMediaType' => 'application/vnd.api+json',
'targetSchema' => $related_schema_uri
->getGeneratedUrl(),
'templatePointers' => [
'instanceHref' => '/links/related/href',
],
'templateRequired' => [
'instanceHref',
],
],
],
],
]);
}, []);
$schema['definitions']['relationships'] = NestedArray::mergeDeep(empty($schema['definitions']['relationships']) ? [] : $schema['definitions']['relationships'], [
'properties' => $relationships,
]);
return $schema;
}