You are here

public static function Data::deduplicate in Drupal 9

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

Returns a new, deduplicated Data object.

Parameters

\Drupal\jsonapi\JsonApiResource\Data $collection: The Data object to deduplicate.

Return value

static A new merged Data object.

File

core/modules/jsonapi/src/JsonApiResource/Data.php, line 167

Class

Data
Represents the `data` and `included` objects of a top-level object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public static function deduplicate(Data $collection) {
  $deduplicated = [];
  foreach ($collection as $resource) {
    $dedupe_key = $resource
      ->getTypeName() . ':' . $resource
      ->getId();
    if ($resource instanceof EntityAccessDeniedHttpException && ($error = $resource
      ->getError()) && !is_null($error['relationship_field'])) {
      $dedupe_key .= ':' . $error['relationship_field'];
    }
    $deduplicated[$dedupe_key] = $resource;
  }
  return new static(array_values($deduplicated));
}