You are here

function node_service_save in Services 6.2

Same name and namespace in other branches
  1. 5 services/node_service/node_service.module \node_service_save()
  2. 6 services/node_service/node_service.inc \node_service_save()
  3. 7 services/node_service/node_service.inc \node_service_save()

Save a node. It creates a new one, in case the 'nid' field is missing.

Parameters

$edit: Array. The node fields' values, just like created on node edit form.

Return value

Number. The node ID.

1 string reference to 'node_service_save'
node_service_service in services/node_service/node_service.module
Implementation of hook_service().

File

services/node_service/node_service.inc, line 120
Link general node functionalities to services module.

Code

function node_service_save($edit) {

  // Load the required includes for drupal_execute
  module_load_include('inc', 'node', 'node.pages');
  $nid = NULL;
  $edit = (array) $edit;

  // The translation module relies on two $_GET variables in order to
  // function properly. In order to properly save translated nodes, we will
  // have to set these by hand when we detect this situation. Summarizing
  // our feelings about having to implement this hack is left as an
  // exercise for the reader.
  if (!empty($edit['tnid']) && !empty($edit['language'])) {
    $_GET['language'] = $edit['language'];
    $_GET['translation'] = $edit['tnid'];
  }
  global $language;
  $languages = language_list();
  $language = isset($languages[$edit['language']]) ? $languages[$edit['language']] : $language;
  if ($edit['nid']) {
    $node = node_load($edit['nid']);
    if ($node->nid) {

      // Setup form_state.
      $form_state = array();
      $form_state['values'] = (array) $edit;
      $form_state['values']['op'] = t('Save');

      // Later on in the chain node_form seems to expect a copy of
      // the old node object.
      $form_state['node'] = (array) $node;
      $ret = drupal_execute($node->type . '_node_form', $form_state, (object) $node);

      // If the node is immediately reloaded after update, it will
      // load the OLD cached version.
      node_load(0, NULL, TRUE);

      // Set $nid, so it can be returned
      $nid = $node->nid;
    }
    else {
      return services_error(t('Node not found'), 401);
    }
  }
  else {

    // If the submitted node has no author (for instance if it was
    // created programmatically by hand) then set the currently logged
    // in user as the author.
    if (!$edit['uid']) {
      global $user;
      $edit['uid'] = $user->uid;
    }

    // Setup form_state
    $form_state = array();
    $form_state['values'] = $edit;
    $form_state['values']['op'] = t('Save');
    $ret = drupal_execute($edit['type'] . '_node_form', $form_state, (object) $edit);

    // Fetch $nid out of $form_state
    $nid = $form_state['nid'];
  }

  // Now unset those $_GET params so they're not hanging around causing trouble
  // for anyone else.
  if (isset($_GET['language'])) {
    unset($_GET['language']);
    unset($_GET['translation']);
  }
  if ($errors = form_get_errors()) {
    return services_error(implode("\n", $errors), 401);
  }
  return $nid;
}