You are here

function _services_raw_node_create in Services Client 7.2

Same name and namespace in other branches
  1. 7 services_raw/services_raw.inc \_services_raw_node_create()

Create node calling raw node_save.

Parameters

$node: Array of node that needs to be saved

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

Code

function _services_raw_node_create($node) {

  // Load original node resource
  module_load_include('inc', 'services', 'resources/node_resource');

  // Adds backwards compatibility with regression fixed in #1083242
  $node = _services_arg_value($node, 'node');

  // Assign username to the node from $user created at auth step.
  if (isset($node['name']) && !isset($node['uid'])) {
    $account = user_load_by_name($node['name']);
    if (!empty($account)) {
      $node['uid'] = $account->uid;
    }
    else {
      unset($node['name']);
    }
  }
  if (!isset($node['name']) && !isset($node['uid'])) {
    global $user;
    $node['name'] = $user->name;
    $node['uid'] = $user->uid;
  }

  // Validate the node. If there is validation error Exception will be thrown
  // so code below won't be executed.
  _node_resource_validate_type($node);

  // Keep original node sent from remote site
  $node_array = $node;
  $keep_properties = array(
    'uid',
    'created',
  );

  // Prepare node object, ensure that we're not going to update some node.
  $node = (object) $node;
  $node->is_new = TRUE;
  unset($node->nid);
  node_object_prepare($node);
  node_submit($node);

  // Force properties from remote site like created, uid
  foreach ($keep_properties as $property) {
    if (isset($node_array[$property])) {
      $node->{$property} = $node_array[$property];
    }
  }
  node_save($node);
  if (!$node->nid) {
    return services_error(t('Error when saving node'), 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;
}