You are here

function cf_node_create in Common Functionality 7

Same name and namespace in other branches
  1. 7.2 modules/cf_node/cf_node.module \cf_node_create()

Programatically creates a node of the form type and returns any errors. This flushes the error buffer allowing for multiple calls. This will always return an array.

Why: There are many arguments of drupal_execute() vs node_save() to the point that I had no idea which was better than the other. See: http://drupal.org/node/178506 See: http://drupal.org/node/131704 See: http://drupal.org/node/293663 See: http://drupal.org/node/604532 See: http://drupal.org/node/530332 See: .. and many many more ..

The solution given here is to use drupal_execute while only needing to populate a single variable.

Parameters

string $form_id: The node type to initialize.

string $node_class: Should be created via the cf_node_initialize_class() function and then altered as necessary.

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

array An array of all errors (if any) that occurred during the creation process.

File

modules/cf_node/cf_node.module, line 86

Code

function cf_node_create($form_id, $node_class, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  if (cf_is_empty_or_non_string($function_history, 'form_id', $form_id, WATCHDOG_ERROR)) {
    return array();
  }
  if (!is_object($node_class)) {
    cf_error_invalid_object($function_history, 'node_class');
    return array();
  }
  $node_state = array(
    'values' => (array) $node_class,
  );
  $node_state['values']['op'] = t('Save');
  if (!cf_has_array_key('name', $node_state['values'])) {
    $current_user = cf_current_user();
    $node_state['values']['name'] = $current_user->name;
  }
  drupal_execute($form_id, $custom_state, $custom_node);
  $form_errors = form_get_errors();
  if (!is_array($form_errors)) {
    $form_errors = array();
  }

  // reset form error array
  form_set_error(NULL, '', TRUE);
  return $form_errors;
}