function EntityResource::doPatchMultipleRelationship

Update a to-many relationship.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The requested entity.

\Drupal\jsonapi\JsonApiResource\ResourceIdentifier[] $resource_identifiers: The client-sent resource identifiers which should be set on the given entity.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition of the entity field to be updated.

1 call to EntityResource::doPatchMultipleRelationship()
EntityResource::doPatchIndividualRelationship in core/modules/jsonapi/src/Controller/EntityResource.php
Update a to-one relationship.

File

core/modules/jsonapi/src/Controller/EntityResource.php, line 763

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function doPatchMultipleRelationship(EntityInterface $entity, array $resource_identifiers, FieldDefinitionInterface $field_definition) {
  $entity->{$field_definition->getName()} = array_map(function (ResourceIdentifier $resource_identifier) {
    // We assume all entity reference fields have an 'entity' computed
    // property that can be used to assign the needed values.
    $field_properties = [
      'entity' => $this->getEntityFromResourceIdentifier($resource_identifier),
    ];
    // Remove `arity` from the received extra properties, otherwise this
    // will fail field validation.
    $field_properties += array_diff_key($resource_identifier->getMeta(), array_flip([
      ResourceIdentifier::ARITY_KEY,
    ]));
    return $field_properties;
  }, $resource_identifiers);
}

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