You are here

public function EntityPut::processRequest in Services 8.4

Same name and namespace in other branches
  1. 9.0.x src/Plugin/ServiceDefinition/EntityPut.php \Drupal\services\Plugin\ServiceDefinition\EntityPut::processRequest()

Return value

\Drupal\Core\Entity\EntityInterface|array

Overrides ServiceDefinitionEntityRequestContentBase::processRequest

File

src/Plugin/ServiceDefinition/EntityPut.php, line 35

Class

EntityPut
Plugin annotation @ServiceDefinition( id = "entity_put", methods = { "PUT" }, translatable = true, deriver = "\Drupal\services\Plugin\Deriver\EntityPut" )

Namespace

Drupal\services\Plugin\ServiceDefinition

Code

public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
  try {
    $updated_entity = parent::processRequest($request, $route_match, $serializer);

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this
      ->getContextValue($this
      ->getDerivativeId());
    if ($entity instanceof ContentEntityInterface) {
      foreach ($updated_entity as $field_name => $field) {
        $entity
          ->set($field_name, $field
          ->getValue());
      }
    }
    else {

      /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $updated_entity */
      foreach ($updated_entity
        ->toArray() as $field_name => $field) {
        $entity
          ->set($field_name, $field);
      }
    }
    $entity
      ->save();
    return $entity
      ->toArray();
  } catch (\Exception $e) {
    throw new HttpException(422, 'The supplied content body could not be serialized into an entity of the requested type.', $e);
  }
}