Ability to add files

mgifford - August 29, 2008 - 16:39
Project:Node factory
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:postponed (maintainer needs more info)
Description

That would be handy. To write some code to parse through a directory and add each file as a node (with the file attached) would be a great addition.

#1

clemens.tolboom - September 3, 2008 - 15:24
Status:active» postponed (maintainer needs more info)

This would be a nice extension.

But could you get me some code example. I haven't done any file upload yet in code that is.

#2

mgifford - September 4, 2008 - 12:55

The image module has a bulk upload tool, but it is specific to images. Seems to do pretty much exactly what would be needed, but it is too specific.

#3

clemens.tolboom - September 4, 2008 - 14:36

If you could find a code example which copies files from /tmp into drupal file system and then returns a file id that would be great.

We do not need a bulk file upload in a node_factory php script. The script is running on the server so there is no need to upload anything. The files are placed somehow in a working directory. Does that make sense?

Cheers

#4

mgifford - September 5, 2008 - 19:32

Don't have time to do this now, but may in the future. Ended up spending the time uploading the files almost by hand. Not nice, but least there were only 200 of them.

#5

j0rd - March 4, 2009 - 03:13
Version:5.x-1.x-dev» 6.x-1.x-dev

Here's a snippet for the D6 version. This won't work for D5. Add into your node_factory.module in the node_factory_set_value function.

<?php
case 'filefield':
// Should be an array of $paths = array('/file/one.txt', '/file/two.txt');
$paths = $value;
$value = array();
foreach(
$paths as $path) {
  
$validators = filefield_widget_upload_validators($field);
  
$files_path = filefield_widget_file_path($field);
  
$value[] = field_file_save_file($path, $validators, $files_path, FILE_EXISTS_RENAME);
}
break;
?>

You can use the code like this

<?php
$edit
= node_factory_create_node('ov_windows');
$files = array('/tmp/blankfile.exe', '/tmp/me.exe');
node_factory_set_value( $edit, 'field_file', $files);
$node_return = node_factory_save_node( $edit);
?>

I've added a bunch of things to my node_factory.module which I'll release once I settle up everything, but posting this snippet for people who can use it in the mean time.

If you're looking for a D5 version, you can take the D5 snippets from here and modify my code to make it work: http://drupal.org/node/201594

#6

clemens.tolboom - March 5, 2009 - 08:06

Nice code ... I hope to have some time to check this code and to commit this.

There is on 'flaw' in that filefield will be the only field that supports multiple values :( Can we fix this for other fields too?

#8

j0rd - March 6, 2009 - 12:52

What "flaw" are you talking about. My code allows for multiple files to get attached into the field.

 
 

Drupal is a registered trademark of Dries Buytaert.