You are here

function _node_resource_create in Services 6.3

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

$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_execute()

2 string references to '_node_resource_create'
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 262
This file will define the resources for dealing directly with nodes

Code

function _node_resource_create($node) {

  // 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.
    global $user;
    $node['name'] = $user->name;
  }

  // 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_type = $node['type'];

  // Load the required includes for drupal_execute
  module_load_include('inc', 'node', 'node.pages');
  $nid = NULL;
  if (module_exists('content') && variable_get('services_use_content_permissions', TRUE)) {
    $type_info = content_types($node['type']);
    $node_object = (object) $node;
    foreach ($type_info['fields'] as $field_name => $field_info) {
      if (isset($node[$field_name]) && !content_access('view', $field_info, $user, $node_object)) {
        unset($node[$field_name]);
      }
    }
  }

  // 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'));
  $ret = drupal_execute($node_type . '_node_form', $form_state, (object) $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;
}