You are here

public function Relationship::getMergedLinks in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/Relationship.php \Drupal\jsonapi\JsonApiResource\Relationship::getMergedLinks()

Merges the object's links with the top-level links.

Parameters

\Drupal\jsonapi\JsonApiResource\LinkCollection $top_level_links: The top-level links to merge.

Return value

\Drupal\jsonapi\JsonApiResource\LinkCollection The merged links.

Overrides TopLevelDataInterface::getMergedLinks

File

core/modules/jsonapi/src/JsonApiResource/Relationship.php, line 192

Class

Relationship
Represents references from one resource object to other resource object(s).

Namespace

Drupal\jsonapi\JsonApiResource

Code

public function getMergedLinks(LinkCollection $top_level_links) {

  // When directly fetching a relationship object, the relationship object's
  // links become the top-level object's links unless they've been
  // overridden. Overrides are especially important for the `self` link, which
  // must match the link that generated the response. For example, the
  // top-level `self` link might have an `include` query parameter that would
  // be lost otherwise.
  // See https://jsonapi.org/format/#fetching-relationships-responses-200 and
  // https://jsonapi.org/format/#document-top-level.
  return LinkCollection::merge($top_level_links, $this
    ->getLinks()
    ->filter(function ($key) use ($top_level_links) {
    return !$top_level_links
      ->hasLinkWithKey($key);
  })
    ->withContext($top_level_links
    ->getContext()));
}