function _services_raw_field_update in Services Client 7
Same name and namespace in other branches
- 7.2 services_raw/services_raw.inc \_services_raw_field_update()
Update entity calling array of _save functions.
Parameters
$uuid: UUID of saved entity
$field: Field of entity that needs to be saved
$entity_type: Currently node or user.
$value: Array of field values that needs to be saved
1 string reference to '_services_raw_field_update'
- _services_raw_services_resources in services_raw/
services_raw.inc - Provides API definition of provided services objects and operations.
File
- services_raw/
services_raw.inc, line 293 - Custom services definition and implementation of all callbacks.
Code
function _services_raw_field_update($uuid, $field, $entity_type, $value) {
switch ($entity_type) {
case 'node':
$id_name = 'nid';
$id = uuid_node_find($uuid);
$save_functions = array(
'node_object_prepare',
'node_save',
);
break;
case 'user':
$id_name = 'uid';
$id = uuid_user_find($uuid);
$save_functions = array(
'user_save',
);
break;
default:
return services_error(t('Entity type @type not supported', array(
'@type' => $entity_type,
)), 404);
}
module_load_include('inc', 'services', 'resources/' . $entity_type . '_resource');
$entity_list = entity_load($entity_type, array(
$id,
));
if (is_array($entity_list)) {
$entity = reset($entity_list);
}
if (empty($entity->{$id_name})) {
return services_error(t('@type @id not found', array(
'@type' => $entity_type,
'@id' => $id,
)), 404);
}
// Validate that the passed field is on this entity
$fields_info = field_info_instances($entity_type, $entity->type);
$match = FALSE;
foreach ($fields_info as $field_name => $field_info) {
if ($field_name == $field) {
$match = TRUE;
continue;
}
}
if (!$match) {
return services_error(t('Field @field not found', array(
'@field' => $field,
)), 404);
}
try {
// Prepare entity defaults
$lang = field_language($entity_type, $entity, $field_name);
$i = 0;
$field_value = NULL;
foreach ($value as $val) {
$field_value[$i]['value'] = $val;
$i++;
}
$entity->{$field}[$lang] = $field_value;
$entity->_services_client['visted'][] = services_client_get_id();
foreach ($save_functions as $function) {
$function($entity);
}
if (empty($entity->{$id_name})) {
return services_error(t('Error when saving entity.'), 406);
}
} catch (Exception $e) {
return services_error(t('Error when saving entity.'), 406, array(
'error' => $e
->getMessage(),
));
}
$result = array(
$id => $entity->{$id_name},
);
if ($uri = services_resource_uri(array(
$entity_type,
$entity->{$id_name},
))) {
$result['uri'] = $uri;
}
return $result;
}