You are here

public static function EntityReferenceFieldNormalizer::getRelationshipLinks in JSON:API 8.2

Gets the links for the relationship.

Parameters

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

string $relationship_field_name: The internal relationship field name.

Return value

array The relationship's links.

2 calls to EntityReferenceFieldNormalizer::getRelationshipLinks()
EntityReferenceFieldNormalizer::normalize in src/Normalizer/EntityReferenceFieldNormalizer.php
Normalizes an object into a set of arrays/scalars.
EntityResource::getRelationship in src/Controller/EntityResource.php
Gets the relationship of an entity.

File

src/Normalizer/EntityReferenceFieldNormalizer.php, line 78

Class

EntityReferenceFieldNormalizer
Normalizer class specific for entity reference field objects.

Namespace

Drupal\jsonapi\Normalizer

Code

public static function getRelationshipLinks(ResourceObject $relationship_context, $relationship_field_name) {
  $resource_type = $relationship_context
    ->getResourceType();
  if ($resource_type
    ->isInternal() || !$resource_type
    ->isLocatable()) {
    return [];
  }
  $public_field_name = $resource_type
    ->getPublicName($relationship_field_name);
  $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;
}