You are here

public function RestWSEntityResourceController::update in RESTful Web Services 7.2

Same name and namespace in other branches
  1. 7 restws.entity.inc \RestWSEntityResourceController::update()

Update an existing resource.

Parameters

int|string $id: The id of the resource that should be updated.

array $values: An array of values for the properties to be updated, keyed by property name.

Overrides RestWSResourceControllerInterface::update

File

./restws.entity.inc, line 202
RESTful web services module integration for entities.

Class

RestWSEntityResourceController
Controller for entity-bases resources.

Code

public function update($id, array $values) {
  $wrapper = $this
    ->wrapper($id);
  $entity_info = $wrapper
    ->entityInfo();

  // Get the ID and bundle property names.
  $entity_keys = array_intersect_key($entity_info['entity keys'], array(
    'id' => 1,
    'bundle' => 1,
  ));
  try {
    foreach ($values as $name => $value) {
      if (in_array($name, $entity_keys)) {

        // We don't allow changing the entity ID or bundle.
        if ($wrapper->{$name}
          ->value() != $value) {
          throw new RestWSException('Unable to change ' . $name, 422);
        }
      }
      else {
        $wrapper->{$name}
          ->set($value);
        if (!$this
          ->checkPropertyAccess($wrapper, $name, $wrapper->{$name})) {
          throw new RestWSException(t('Not authorized to set property @p', array(
            '@p' => $name,
          )), 403);
        }
      }
    }
  } catch (EntityMetadataWrapperException $e) {
    throw new RestWSException($e
      ->getMessage(), 406);
  }
  $this
    ->validateFields($wrapper);
  $wrapper
    ->save();
}