Question

I'm trying to write PHP to insert lots of content into my website's drupal 6 database. The content type has got 3 custom fields:

field_theme_preview_demo_link - text field

field_theme_preview_content_styl - text selector

field_theme_preview_thumb - image field with an 'alt'

I just copied the images over to the correct directory manually.

I created the content type (and manually inserted some content to confirm it was all ok).

I then found some code sample for programatically inserting content...

Creating Drupal CCK content programatically/ via API

and http://drupal.org/node/458778#comment-1653696 (for this one I couldnt understand the mod that is suggested for D6).

I tried to modify it but couldnt get it to work - it does create the new content but the image field is blank. (To run this code I just paste into the 'Execute PHP Code' block.)

global $user;
$newnode = new stdClass();
$newnode->title = 'New node title';
$newnode->body = "the body";
$newnode->uid = $user->uid;
$newnode->type = 'theme_preview';
$newnode->status = 1;
$newnode->promote = 0;
$newnode->active = 1;

$newnode->field_theme_preview_demo_link[0]['value'] = 'the link';
$newnode->field_theme_preview_content_styl[0]['value'] = 'Books';

$newnode->field_theme_preview_thumb = array(
    array(
      'fid' => 'upload',
      'alt' => 'the alt',
      'filename' =>  'sites/default/files/theme_preview_thumbs/41531.jpg',
      'filepath' => 'sites/default/files/theme_preview_thumbs',
      'filesize' => filesize('sites/default/files/theme_preview_thumbs/41531.jpg'),
    ),
  );

node_save($newnode);
Was it helpful?

Solution 2

Use http://drupal.org/project/node_export this module to import or export content to particular content type, it really works for me and also image will be uploaded automatically.

One thing you have to consider for using this module, after importing nodes, make sure that images are matched with its content.

Using this module i have uploaded 100s of news article to my website from my local server site.

OTHER TIPS

Setting the fid field to 'upload' is something that is done by a node's form when there actually is a file being uploaded. If you are programatically generating content, you will need to fetch the fid from somewhere else.

You will probably need to manually use the file api to enter a file from somewhere on the filesystem into the file database, and then draw your fid from the resulting object.

@Seth Battin, @ Ayesh K, thanks for the tips, working code...

global $user;
$newnode = new stdClass();
$newnode->title = 'New node title';
$newnode->body = "this is a new node, created by import function";
$newnode->uid = $user->uid;
$newnode->type = 'theme_preview';
$newnode->status = 1;
$newnode->promote = 0;
$newnode->active = 1;

$newnode->field_theme_preview_demo_link[0]['value'] = 'test 1';
$newnode->field_theme_preview_content_styl[0]['value'] = 'Books';

$field = field_file_save_file(
    'sites/default/files/srcImage1.jpg', 
    array(),
    'sites/default/files/theme_preview_thumbs');

if( !isset($field['data']) )  $field['data'] = array();
$field['data']['title'] = "the image title";
$field['data']['alt'] = "the image alt";

$newnode->field_theme_preview_thumb = array(0 => $field);
$newnode = node_submit($newnode);
node_save($newnode);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top