You are here

public function DataProviderEntity::update in RESTful 7.2

Update operation.

Parameters

mixed $identifier: The ID of thing to be updated.

mixed $object: The thing that will be set.

bool $replace: TRUE if the contents of $object will replace $identifier entirely. FALSE if only what is set in $object will replace those properties in $identifier.

Return value

array An array of structured data for the thing that was updated.

Overrides CrudInterface::update

File

src/Plugin/resource/DataProvider/DataProviderEntity.php, line 290
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.

Class

DataProviderEntity
Class DataProviderEntity.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

public function update($identifier, $object, $replace = FALSE) {
  $this
    ->validateBody($object);
  $entity_id = $this
    ->getEntityIdByFieldId($identifier);
  $this
    ->isValidEntity('update', $entity_id);

  /* @var \EntityDrupalWrapper $wrapper */
  $wrapper = entity_metadata_wrapper($this->entityType, $entity_id);
  $this
    ->setPropertyValues($wrapper, $object, $replace);

  // Set the HTTP headers.
  $this
    ->setHttpHeader('Status', 201);
  if (!empty($wrapper->url) && ($url = $wrapper->url
    ->value())) {
    $this
      ->setHttpHeader('Location', $url);
  }

  // The access calls use the request method. Fake the view to be a GET.
  $old_request = $this
    ->getRequest();
  $this
    ->getRequest()
    ->setMethod(RequestInterface::METHOD_GET);
  $output = array(
    $this
      ->view($identifier),
  );

  // Put the original request back to a PUT/PATCH.
  $this->request = $old_request;
  return $output;
}