You are here

public static function EntityReferenceFieldNormalizer::getRelationshipLinks in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Normalizer/EntityReferenceFieldNormalizer.php \Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer::getRelationshipLinks()

Gets the links for the relationship.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObject $relationship_context: The JSON:API resource object context of the relationship.

\Drupal\jsonapi\ResourceType\ResourceTypeRelationship $resource_relationship: The resource type relationship field.

Return value

array The relationship's links.

1 call to EntityReferenceFieldNormalizer::getRelationshipLinks()
EntityReferenceFieldNormalizer::normalize in core/modules/jsonapi/src/Normalizer/EntityReferenceFieldNormalizer.php

File

core/modules/jsonapi/src/Normalizer/EntityReferenceFieldNormalizer.php, line 76

Class

EntityReferenceFieldNormalizer
Normalizer class specific for entity reference field objects.

Namespace

Drupal\jsonapi\Normalizer

Code

public static function getRelationshipLinks(ResourceObject $relationship_context, ResourceTypeRelationship $resource_relationship) {
  $resource_type = $relationship_context
    ->getResourceType();
  if ($resource_type
    ->isInternal() || !$resource_type
    ->isLocatable()) {
    return [];
  }
  $public_field_name = $resource_relationship
    ->getPublicName();
  $relationship_route_name = Routes::getRouteName($resource_type, "{$public_field_name}.relationship.get");
  $links = [
    'self' => Url::fromRoute($relationship_route_name, [
      'entity' => $relationship_context
        ->getId(),
    ]),
  ];
  if (static::hasNonInternalResourceType($resource_type
    ->getRelatableResourceTypesByField($public_field_name))) {
    $related_route_name = Routes::getRouteName($resource_type, "{$public_field_name}.related");
    $links['related'] = Url::fromRoute($related_route_name, [
      'entity' => $relationship_context
        ->getId(),
    ]);
  }
  if ($resource_type
    ->isVersionable()) {
    $version_query_parameter = [
      JsonApiSpec::VERSION_QUERY_PARAMETER => $relationship_context
        ->getVersionIdentifier(),
    ];
    $links['self']
      ->setOption('query', $version_query_parameter);
    if (isset($links['related'])) {
      $links['related']
        ->setOption('query', $version_query_parameter);
    }
  }
  return $links;
}