Programmatically Creating CCK Nodes and Adding Images with Imagefield
Submitted by Darren on Wed, 23/04/2008 - 09:58.
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