You are here

function _services_raw_node_attach_file in Services Client 7

Same name and namespace in other branches
  1. 7.2 services_raw/services_raw.inc \_services_raw_node_attach_file()

Attach file object to node filefield

Parameters

$nid: Node id

$field_name: Field name to be updated with attached file

$fid: File object id

1 string reference to '_services_raw_node_attach_file'
_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 616
Custom services definition and implementation of all callbacks.

Code

function _services_raw_node_attach_file($nid, $field_name, $fid) {

  // Check if uploaded file exists
  if ($file = file_load($fid)) {
    $file->display = 1;
    $file->description = '';
    $node = node_load($nid);
    if (!isset($node->{$field_name})) {

      // Do not allow to update not existing node field
      services_error(t("The field %field_name doesn't exist", array(
        '%field_name' => $field_name,
      )), 403);
    }
    $node->revision = '';
    $node->{$field_name}['und'][] = (array) $file;
    node_save($node);
  }
  else {
    return services_error(t("The file fid %fid could not be attached, because it doesn't exist.", array(
      '%fid' => $fid,
    )), 406);
  }

  // Only add the URI for servers that support it.
  $result = array(
    'nid' => $node->nid,
  );
  if ($uri = services_resource_uri(array(
    'node',
    $node->nid,
  ))) {
    $result['uri'] = $uri;
  }
  return $result;
}