cf_node.module in Common Functionality 7
Same filename and directory in other branches
File
modules/cf_node/cf_node.moduleView source
<?php
/**
* Create a node class object.
*
* This is designed to be used for cf_node_create() or drupal_execute()
* functions.
* This generates the minimum fields required.
*
* Why:
* See why from cf_node_create() function.
*
* @param string $node_type
* The node type to initialize.
* @param string $node_title
* The note title to use.
* @param string $node_body
* (optional) The node body to use.
* @param array $function_history
* (optional) An array of function names, ie:
* array('0' => 'my_function_name').
*
* @return object
* A basic node object that can be safely passed to drupal_execute().
*/
function cf_node_initialize_class($node_type, $node_title, $node_body = '', array $function_history = array()) {
cf_error_append_history($function_history, __FUNCTION__);
$node_class = new stdClass();
if (cf_is_empty_or_non_string($function_history, 'node_type', $node_type, WATCHDOG_ERROR)) {
return $node_class;
}
if (cf_is_empty_or_non_string($function_history, 'node_title', $node_title, WATCHDOG_ERROR)) {
return $node_class;
}
if (!is_string($node_body)) {
$node_body = '';
}
// node_object_prepare requires the node.pages.inc be loaded to be called
module_load_include('inc', 'node', 'node.pages');
node_object_prepare($node_class);
$node_class->type = $node_type;
$node_class->title = $node_title;
$node_class->body = $node_body;
$node_class->active = TRUE;
return $node_class;
}
/**
* 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.
*
* @param string $form_id
* The node type to initialize.
* @param string $node_class
* Should be created via the cf_node_initialize_class() function and then
* altered as necessary.
* @param array $function_history
* (optional) An array of function names, ie:
* array('0' => 'my_function_name').
*
* @return array
* An array of all errors (if any) that occurred during the creation process.
**/
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;
}
Functions
Name | Description |
---|---|
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. |
cf_node_initialize_class | Create a node class object. |