function Data::deduplicate

Same name in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/Data.php \Drupal\jsonapi\JsonApiResource\Data::deduplicate()
  2. 10 core/modules/jsonapi/src/JsonApiResource/Data.php \Drupal\jsonapi\JsonApiResource\Data::deduplicate()
  3. 11.x 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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.