2

I have a very straight forward content type created in Drupal - Title, Body, Taxonomy term (drop down select, 1 value only), and Photos (multiple permitted, up to 8, alt and title tags are enabled). I also have the meta

I have a set of CSV files with data for those fields with corresponding photos. I need to iterate over these programatically and use node_save() to get these in the database as well as upload the related photos.

I followed the direction here, and while it was a good start, I am still confused as to how to handle multiple uploads.

Bulk saving nodes with a file field

Can any suggest a tutorial or provide some sample code that can better illustrate how to do this?

EDIT: Based on the direction below, this is the code I have come up with

$images = array();
foreach ($file_data_array as $csv_data) {
    foreach($image_array as $file_data) {
  $file = new stdClass();
  $file->filename = basename($file_data['filepath']);
  $file->filepath = $file_data['filepath'];
  $file->filemime = $file_data['mime'];
  $file->filesize = filesize($file_data['filepath']);
  $file->uid = $uid;
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  $images[] = array(
    'fid' => $file->fid,
    'alt' => $file_data['alt'],
    'title' => $file_data['title']
  );
}
$node = new stdClass;
$node->type = 'baby_clothes';
$node->body = 'body';
$node->title = 'title';
$node->uid = 1;
$node->status = 1;
$node->active = 1;

//the name of my actual field is 'product images'
$node->field_product_images[LANGUAGE_NONE] = $images;

//this is my actual taxonomy field
$node->field_baby_clothes_category[LANGUAGE_NONE][0]['tid'] = 2; //actual ID of the taxonomy for this node

node_save($node);
}

Thoughts?

4

You would normally just build up the files in a loop like so:

$images = array();

foreach ($file_data_array as $file_data) {
  $file = new stdClass();
  $file->filename = basename($file_data['filepath']);
  $file->filepath = $file_data['filepath'];
  $file->filemime = $file_data['mime'];
  $file->filesize = filesize($file_data['filepath']);

  $file->uid = $uid;
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);

  $images[] = array(
    'fid' => $file->fid,
    'alt' => $file_data['alt'],
    'title' => $file_data['title']
  );
}

$node = new stdClass;
// Build up the rest of the node as normal

$node->field_images[LANGUAGE_NONE] = $images;

node_save($node);

This assumes you have your CSV data loaded into an array called $file_data_array.

fid, alt, title, height and width are the only columns available to an image field type so there's no need to build up an array for each field entry containing all the file details (you can ignore that bit in the other answer you linked to as it's unnecessary).

Also note the use of file_save() instead of drupal_write_record() to commit the file to the database...it's much better to use file_save() as it fires the necessary system hooks to let other modules react to your files being added.

Hope that helps

  • Thanks, that is helping quite a bit. One thing I believe is confusing me, I guess I don't have to worry about writing the actual images files anywhere? What I mean is, this all looks like it just references a file. After these process, I just upload all of my files to the sites/default/files directory and let Drupal take care of the rest? – blue928 Feb 16 '12 at 22:23
  • Please see the above code I added in my edit. Clive, to take care of the image array, did you mean that the images array needed its on foreach loop within the foreach loop that iterates through the CSV file? – blue928 Feb 16 '12 at 23:30
1

You need to create a dummy node normally and then look at the saved $node array to get an example.

Try the below in a block while you view that page

<pre><?php print_r($node) ?></pre>

Once you can see the format of the data you can then write the code required to save the picture fields. They will probably be a multi dimensional array with things like the alt and title tags set etc...

Post up the $node array in your question for more advice

  • Ah, thank you for that, very helpful to see what is available to me via that function. – blue928 Feb 16 '12 at 22:25
  • For these purposes, it's better to use dpm() function that the Devel module provides, it makes browsing through your object or array much much easier. – Bálint Deáki Jul 12 '13 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.