You are here

protected function EntityResource::updateEntityField in JSON:API 8

Same name and namespace in other branches
  1. 8.2 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\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.

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

File

src/Controller/EntityResource.php, line 956

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function updateEntityField(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.
    try {
      $field_name = $this->resourceType
        ->getInternalName($field_name);
      $destination_field_list = $destination
        ->get($field_name);
    } catch (\InvalidArgumentException $e) {
      $resource_type = $this->resourceTypeRepository
        ->get($destination
        ->getEntityTypeId(), $destination
        ->bundle());
      throw new UnprocessableEntityHttpException(sprintf('The attribute %s does not exist on the %s resource type.', $field_name, $resource_type
        ->getTypeName()));
    }
    $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.');
  }
}