You are here

function _node_resource_update in Services 7.3

Same name and namespace in other branches
  1. 6.3 resources/node_resource.inc \_node_resource_update()
  2. 6.2 services/node_service/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_form_submit() 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_form_submit()

3 string references to '_node_resource_update'
hook_services_resources in docs/services.services.api.php
Defines function signatures for resources available to services.
ServicesRESTServerTests::getTestResource in servers/rest_server/tests/ServicesRESTServerTests.test
_node_resource_definition in resources/node_resource.inc

File

resources/node_resource.inc, line 397

Code

function _node_resource_update($nid, $node) {

  // Adds backwards compatability with regression fixed in #1083242
  $node = _services_arg_value($node, 'node');
  $node['nid'] = $nid;
  $old_node = node_load($nid);
  if (empty($old_node->nid)) {
    return services_error(t('Node @nid not found', array(
      '@nid' => $old_node->nid,
    )), 404);
  }

  // If no type is provided use the existing node type.
  if (empty($node['type'])) {
    $node['type'] = $old_node->type;
  }
  elseif ($node['type'] != $old_node->type) {

    // Node types cannot be changed once they are created.
    return services_error(t('Node type cannot be changed'), 406);
  }

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

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

  // 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['programmed_bypass_access_check'] = FALSE;

  // Contributed modules may check the triggering element in the form state.
  // The triggering element is usually set from the browser's POST request, so
  // we'll automatically set it as the submit action from here.
  $stub_form = drupal_get_form($node_type . '_node_form', (object) $old_node);
  $form_state['triggering_element'] = $stub_form['actions']['submit'];
  drupal_form_submit($node_type . '_node_form', $form_state, $old_node);
  if ($errors = form_get_errors()) {
    return services_error(implode(" ", $errors), 406, array(
      'form_errors' => $errors,
    ));
  }
  $node = array(
    'nid' => $nid,
  );
  if ($uri = services_resource_uri(array(
    'node',
    $nid,
  ))) {
    $node['uri'] = $uri;
  }
  return $node;
}