You are here

public function ServicesEntityResourceControllerClean::update in Services Entity API 7.2

Implements ServicesResourceControllerInterface::update().

Overrides ServicesEntityResourceController::update

File

plugins/services_entity_resource_clean.inc, line 79

Class

ServicesEntityResourceControllerClean
This class is designed to create a very clean API that integrates with the services and entity modules. We want to strip all "drupalisms" out of the API. For example, there should be no [LANGUAGE_NONE][0][value] or field_ in the API.

Code

public function update($entity_type, $entity_id, array $values) {
  $property_info = entity_get_all_property_info($entity_type);
  $values = $this
    ->transform_values($entity_type, $property_info, $values);
  try {
    $wrapper = entity_metadata_wrapper($entity_type, $entity_id);
    foreach ($values as $name => $value) {

      // Only attempt to set properties when the new value differs from that
      // on the existing entity; otherwise, requests will fail for read-only
      // and unauthorized properties, even if they are not being changed. This
      // allows us to UPDATE a previously retrieved entity without removing
      // such properties from the payload, as long as they are unchanged.
      if (!$this
        ->propertyHasValue($wrapper, $name, $value)) {

        // We set the property before checking access so the new value
        // will be passed to the access callback. This is necesssary in
        // some cases (e.g. text-format fields) where access permissions
        // depend on the value that is being set.
        $wrapper->{$name}
          ->set($value);
        if (!$this
          ->propertyAccess($wrapper, $name, 'update')) {
          services_error(t("Not authorized to set property '@property-name'.", array(
            '@property-name' => $name,
          )), 403);
        }
      }
    }
  } catch (EntityMetadataWrapperException $e) {
    services_error($e
      ->getMessage(), 406);
  }
  $wrapper
    ->save();
  return $this
    ->get_data($wrapper, '*');
}