Alimozzaman

Posts Tagged ‘drupal

1. 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);

2. To save multiple images with node

3.  global $user;

4.  // Build the node.

5.  $node = new stdClass();

6.  $node->type = $node_type;

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

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

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

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

11.$files = array();

12.

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

14.

15.// Image file path.

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

17.

18.// 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.

19.

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

21.

22.// Load up the appropriate validators

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

24.

25.// Store file path.

26.$files_path = filefield_widget_file_path($field);

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

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

29.

30.$files[$i] = $file;

31.

32.}

33.

34.// put the files into node image field.

35.$node->field_image = $files;

36.

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

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

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

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

41.if (module_exists(‘comment’)) {

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

43.    }

44.

45.$node = node_submit($node);

46.node_save($node);

Tags: