function EntityResource::updateEntityField

Same name in other branches
  1. 9 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::updateEntityField()
  2. 8.9.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::updateEntityField()
  3. 10 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::updateEntityField()

Takes a field from the origin entity and puts it to the destination entity.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type of the entity to be updated.

\Drupal\Core\Entity\EntityInterface $origin: The entity that contains the field values.

\Drupal\Core\Entity\EntityInterface $destination: The entity that needs to be updated.

string $field_name: The name of the field to extract and update.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown when the serialized and destination entities are of different types.

1 call to EntityResource::updateEntityField()
EntityResource::patchIndividual in core/modules/jsonapi/src/Controller/EntityResource.php
Patches an individual entity.

File

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

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function updateEntityField(ResourceType $resource_type, EntityInterface $origin, EntityInterface $destination, $field_name) {
    // The update is different for configuration entities and content entities.
    if ($origin instanceof ContentEntityInterface && $destination instanceof ContentEntityInterface) {
        // First scenario: both are content entities.
        $field_name = $resource_type->getInternalName($field_name);
        $destination_field_list = $destination->get($field_name);
        $origin_field_list = $origin->get($field_name);
        if ($this->checkPatchFieldAccess($destination_field_list, $origin_field_list)) {
            $destination->set($field_name, $origin_field_list->getValue());
        }
    }
    elseif ($origin instanceof ConfigEntityInterface && $destination instanceof ConfigEntityInterface) {
        // Second scenario: both are config entities.
        $destination->set($field_name, $origin->get($field_name));
    }
    else {
        throw new BadRequestHttpException('The serialized entity and the destination entity are of different types.');
    }
}

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