(Tutorial) Programmatically Create, Insert, and Update CCK Nodes
Tutorial : Programmatically Create, Insert, and Update CCK Nodes
It has posed three important development hurdles.
Create Content Types
Export the content type from CCK (admin/content/types/export), save the array in an array, and then pass it to the function below. This improves upon the previously known techniques, in that the exported CCK types no longer need to be escaped.
function _install_create_content($content) {
$type = $content['type']['type'];
global $_install_macro;
$_install_macro[$type] = $content;
include_once './'. drupal_get_path('module', 'node') .'/content_types.inc';
include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
$macro = 'global $_install_macro; $content = $_install_macro['. $type .'];';
drupal_execute('content_copy_import_form',
array('type_name' => '', 'macro' => $macro));
content_clear_type_cache();
}
Inserting Nodes
See Quick and Dirty CCK Imports.
The correct way to insert and update nodes is with drupal_execute, rather than node_submit because it handles form alter and form validation.
Debug the node edit form by placing a print_r($node) in node.module at the top of node_submit, edit node/add/yourtype, submit the form, remove your debugging print_r, then create your values array to match these values.
$node->type = 'yourtype';
$values = array();
$values[...] = ...;
drupal_execute('yourtype_node_form', $values, $node);
$errors = form_get_errors();
if (count($errors)) {
// do something ...
}
Updating Nodes
When updating nodes, you'll need to load the node using node_load, but then before updating the values, the default values need to be pre-populated from the node.
Courtesy : Civicactions.com
- guru's blog
- Login to post comments
![Drupal-6-Book-[Building Powerful and Robust Websites with Drupal 6].jpg](http://www.drupalranch.com/images/Drupal-6-Book-[Building%20Powerful%20and%20Robust%20Websites%20with%20Drupal%206].jpg)