function EntityResource::doPatchMultipleRelationship

Same name in other branches
  1. 9 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::doPatchMultipleRelationship()
  2. 10 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::doPatchMultipleRelationship()
  3. 11.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\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 713

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function doPatchMultipleRelationship(EntityInterface $entity, array $resource_identifiers, FieldDefinitionInterface $field_definition) {
    $main_property_name = $field_definition->getItemDefinition()
        ->getMainPropertyName();
    $entity->{$field_definition->getName()} = array_map(function (ResourceIdentifier $resource_identifier) use ($main_property_name) {
        $field_properties = [
            $main_property_name => $this->getEntityFromResourceIdentifier($resource_identifier)
                ->id(),
        ];
        // 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.