Programmatically Creating Nodes With CCK Imagefields For Drupal 6 | Xplantr

Programmatically creating nodes with CCK ImageFields for Drupal 6

To save a single image with node.

global $user;

// Build the node.

$node = new stdClass();

$node->type = $node_type;

$node->uid = $user->uid;

$node->name = $user->name;

$node->title = isset($title) ? $title : $name_place;

$node->body = isset($body) ? $body : $body_content;

// Image file path. $image = file_directory_path().’/ example.jpg’’;

// Load up the CCK field.

First parameter image field name and second parameter node type. It can be chnaged to any field name and also and node type.

$field = content_fields(‘field_image’,“page”);

// Load up the appropriate validators

$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));

// Store file path.

$files_path = filefield_widget_file_path($field);

// Create the file object, replace existing file with new file as source and dest are the same

$file = field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

// put the file into node image field.

$node->field_image = array( 0 => $file);

// Set the node’s defaults… (copied this from node and comment.module)

$node_options = variable_get(‘node_options_’. $node->type, array(‘status’, ‘promote’));

$node->status = in_array(‘status’, $node_options);

$node->promote = in_array(‘promote’, $node_options);

if (module_exists(‘comment’)) {

$node->comment = variable_get(“comment_$node->type”, COMMENT_NODE_READ_WRITE); }

$node = node_submit($node);

node_save($node);

To save multiple images with node

global $user;

// Build the node.

$node = new stdClass();

$node->type = $node_type;

 $node->uid = $user->uid;

$node->name = $user->name;

$node->title = isset($title) ? $title : $name_place;

 $node->body = isset($body) ? $body : $body_content;

$files = array();

for($i =0;$i<10;$i++){

// Image file path.

$image = file_directory_path().’/example.jpg’;

// Load up the CCK field. First parameter image field name and second parameter node type. It can be chnaged to any field name and also and node type.

$field = content_fields(‘field_image’,“page”);

// Load up the appropriate validators

$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));

// Store file path.

$files_path = filefield_widget_file_path($field);

// Create the file object, replace existing file with new file as source and dest are the same

$file = field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

$files[$i] = $file; }

// put the files into node image field.

$node->field_image = $files;

// Set the node’s defaults… (copied this from node and comment.module)

$node_options = variable_get(‘node_options_’. $node->type, array(‘status’, ‘promote’));

$node->status = in_array(‘status’, $node_options);

$node->promote = in_array(‘promote’, $node_options);

if (module_exists(‘comment’)) {

$node->comment = variable_get(“comment_$node->type”, COMMENT_NODE_READ_WRITE); }

$node = node_submit($node);

node_save($node);