You are here

function _node_resource_update in Services 6.3

Same name and namespace in other branches
  1. 6.2 services/node_service/node_resource.inc \_node_resource_update()
  2. 7.3 resources/node_resource.inc \_node_resource_update()
  3. 7 services/node_service/node_resource.inc \_node_resource_update()

Updates a new node based on submitted values.

Note that this function uses drupal_execute() to create new nodes, which may require very specific formatting. The full implications of this are beyond the scope of this comment block. The Googles are your friend.

Parameters

$nid: Node ID of the node we're editing.

$node: Array representing the attributes a node edit form would submit.

Return value

The node's nid.

See also

drupal_execute()

2 string references to '_node_resource_update'
hook_services_resources in ./services.services.api.php
Implementation of hook_services_resources(). Defines function signatures for resources available to services.
_node_resource_definition in resources/node_resource.inc
@file This file will define the resources for dealing directly with nodes

File

resources/node_resource.inc, line 348
This file will define the resources for dealing directly with nodes

Code

function _node_resource_update($nid, $node) {

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

  // Validate the node. If there is validation error Exception will be thrown
  // so code below won't be executed.
  _node_resource_validate_type($node);
  $node['nid'] = $nid;
  $old_node = node_load($nid);
  if ($old_node->nid) {

    // Node types cannot be changed once they are created.
    if (isset($node['type']) && $node['type'] != $old_node->type) {
      return services_error(t('Node type cannot be changed'), 406);
    }

    // Load the required includes for drupal_execute
    module_load_include('inc', 'node', 'node.pages');
    $node_type = $node['type'];

    // Setup form_state.
    $form_state = array();
    $form_state['values'] = $node;
    $form_state['values']['op'] = variable_get('services_node_save_button_' . $node_type . '_resource_update', t('Save'));
    $form_state['node'] = (array) $old_node;
    drupal_execute($node_type . '_node_form', $form_state, $old_node);
    if ($errors = form_get_errors()) {
      return services_error(implode(" ", $errors), 406, array(
        'form_errors' => $errors,
      ));
    }
  }
  else {
    return services_error(t('Node not found'), 404);
  }
  $node = array(
    'nid' => $nid,
  );
  if ($uri = services_resource_uri(array(
    'node',
    $nid,
  ))) {
    $node['uri'] = $uri;
  }
  return $node;
}