You are here

function _node_resource_create in Services 7.3

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

Creates 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

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

Return value

An associative array contained the new node's nid and, if applicable, the fully qualified URI to this resource.

See also

drupal_form_submit()

3 string references to '_node_resource_create'
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 295

Code

function _node_resource_create($node) {
  global $user;

  // Adds backwards compatability with regression fixed in #1083242
  $node = _services_arg_value($node, 'node');
  if (!isset($node['name'])) {

    // Assign username to the node from $user created at auth step.
    if (isset($user->name)) {
      $node['name'] = $user->name;
    }
  }
  if (!isset($node['language'])) {
    $node['language'] = LANGUAGE_NONE;
  }

  // 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'];

  // Setup form_state
  $form_state = array();
  $form_state['values'] = $node;
  $form_state['values']['op'] = variable_get('services_node_save_button_' . $node_type . '_resource_create', t('Save'));
  $form_state['programmed_bypass_access_check'] = FALSE;
  $form_state['no_cache'] = TRUE;

  // Build a stub node object for the form in a similar way as node_add() does,
  // but always make the node author default to the current user (if the user
  // has permission to change it, $form_state['values'] will override this
  // default when the form is submitted).
  $stub_node = (object) array_intersect_key($node, array_flip(array(
    'type',
    'language',
    'tnid',
  )));
  $stub_node->name = $user->name;

  // 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_state = array(
    'no_cache' => TRUE,
    'build_info' => array(
      'args' => array(
        (object) $stub_node,
      ),
    ),
  );
  $stub_form = drupal_build_form($node_type . '_node_form', $stub_form_state);
  $form_state['triggering_element'] = $stub_form['actions']['submit'];
  drupal_form_submit($node_type . '_node_form', $form_state, (object) $stub_node);
  if ($errors = form_get_errors()) {
    return services_error(implode(" ", $errors), 406, array(
      'form_errors' => $errors,
    ));
  }

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

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