How to attach an image to a node when creating it using node_save ($ node);

Hi, I am working with drupal 7 and trying to import data from xml, analyzing it with php and then creating nodes with node_save($node) .

So far, I have managed to create nodes from xml without any images. I want to attach an image to a node while I import it.

I know drupal 7 is still in alpha 6, but that's good. node_save($node) function is almost the same as in drupal 6, but slightly different.

Ok, here is my path to the image code file stored in a variable ... any help would be great. thanks in advance

 function main($head, $txt) { $nodes = array(); $nodes[0]['title'] = $head; // node name $nodes[0]['body'] = $txt; // body text for the node $nodes[0]['teaser'] = '<p>A node</p>'; $nodes[0]['timestamp'] = 1281765101; $nodes[0]['format'] = 2; make_nodes($nodes); } function make_nodes($nodes) { $new_node = $nodes[0]; $node = new stdClass(); $node->type = 'article'; $node->status = 1; $node->uid = 1; $node->title = $new_node['title']; $node->promote = 1; $node->created = $new_node['timestamp']; $node->timestamp = $new_node['timestamp']; $node->changed= $new_node['timestamp']; $node->sticky = 0; $node->language = 'en'; $node->body['und'][0]['format'] = 3; $node->body['und'][0]['summary'] = $new_node['teaser']; $node->body['und'][0]['value'] = $new_node['body']; $node->revision = 0; node_submit($node); node_save($node); } 
+4
source share
2 answers

Hi. After reading the documentation for 10 hours, I finally did this ... I am including my code here.

 $uri = 'bird/bird_image.jpg'; $files = new stdClass(); $files->uid = (isset($local_user->uid) && !empty($local_user->uid)?$local_user->uid:1); $files->filename = 'bird.jpg'; $files->uri = $uri; $files->filemime = file_get_mimetype($uri); $files->status = 1; $files->timestamp = $new_node['timestamp']; file_copy($files); 

Here's how to download drupal 7 file and database

+2
source

The original question that Zero Cool asked was "How to attach an image to a node when creating it."

I have not seen it fully respond anywhere, so here is the code that works for me in Drupal 7.27, on Windows 8 :)

The "classified" content type for this node has 3 fields: title, body and field_advert_image (which is only the image field type).

 $node = new stdClass(); $node->type = 'classified'; node_object_prepare($node); // Set some default values. $node->language = LANGUAGE_NONE; $node->title = "Test node with image"; $node->body[LANGUAGE_NONE][0]['value'] = strip_tags("<b>Body text example</b>"); $node->body[LANGUAGE_NONE][0]['format'] = 'plain_text'; // or 'full_html' if you want // Get pathauto to generate an URL alias $node->path['pathauto'] = 1; // Created by user 1 $node->uid = 1; // The image directory has to be visible to Drupal on its local file-system, // eg here it would be sites/default/files/adverts $directory_uri = 'public://adverts'; // example image file in that directory: $my_image_file = '32449.jpg'; // First create a file object, and add it to Drupal managed_files table... $file = new stdClass(); $file->filename = $my_image_file; $file->uri = $directory_uri.'/'.$my_image_file; $file->filemime = file_get_mimetype($file->uri); $file->status = FILE_STATUS_PERMANENT; $file->uid = 1; $file = file_save($file); // ...then use the new file object. $node->field_advert_image[LANGUAGE_NONE][0] = (array)$file; node_save($node); // NB node_save() gets the count incremented in the file_managed table; no need to call file_usage_add() if ($node->nid) { echo "- Created node ".$node->nid." ... ".print_r($node,true)."\n"; } else { echo "- Drupal node_save API call failed\n"; } 

As you probably guessed, this code is executed from the command line PHP script.

Hope this helps someone.

0
source

All Articles