Programmatically Creating CCK Nodes and Adding Images with Imagefield

in

There's a useful post on civicactions that explains how to create nodes in your Drupal site programmatically. I'm using this technique to create an import script for processing bulk quantities of XML files.

There's a facet to the create node process, in that I couldn't get it to import images using the $values[] array.

I found that I could create all the CCK fields by assigning them fields in the $value[] array, but for imagefield values I had to instantiate them in the $node object instead.

Here's my code for importing imagefields, taken from the XML/JPEG import script:

<?php
  $node
->type = 'album';
 
$node->field_cover_image = array(
    array(
     
'fid' => 'upload',
     
'title' => (string)$xml->album->album_title,
     
'filename' => $img_file->filename,
     
'filepath' => $img_file->filepath,
     
'filesize' => $img_file->filesize,
    ),
  );
 
$values = array();
 
$values['field_album_upc'][0]['value'] = (string)$xml->album->album_upc;
 
$values['title'] = (string)$xml->album->album_title;
 
$values[...] = ...; // rest of CCK fields created here
 
drupal_execute('album_node_form', $values, $node);
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options