Hi,

I'm trying to create nodes programmatically thanks to drupal_execute() command. No problem, I succeed making some nodes with "basic" fields, and with Node Reference fields for example, but I have a problem with a Filefield (ImageField to be specific).

I explain. I have a in a x folder already in the server some pictures that I want to attach to my node. I followed the instructions based on many already solved issues like http://drupal.org/node/330421 but id doesn't work...
I got this message each time I try: "Referencing to the file used in the field is not allowed.", and I really don't understand since this issue seemed to be caused by old versions of Filefield and/or ImageField, but in my case these modules are updated.

Another point, some of the nodes may have the url of the same image...

To finish, I was wondering if it was a problem of the $status set in my drupal_add_existing_file function, but I tried changing this status and I had the same error.
I was wondering too if it was caused by the "drupal_write_record" which put the $file in reference, but for other persons it works.. Then I don't know why it wouldn't work for me...

Can you help me please?

My code:

function drupal_add_existing_file($file_drupal_path,$uid=1,$status=FILE_STATUS_PERMANENT) {
	$file=(object) array(
		'filename' => basename($file_drupal_path),
		'filepath' => $file_drupal_path,
		'filemime' => file_get_mimetype($file_drupal_path),
		'filesize' => filesize($file_drupal_path),
		'uid' => $uid,
		'status' => $status,
		'timestamp' => time()
	);
	drupal_write_record('files',$file); 
	
	return field_file_load($file_drupal_path);
}
function generateAlbum($rdata, $nid) {
	
	global $user;

	$file_drupal_path = utf8_encode($rdata[5]);

	$node = array(
	'uid' => (string) $user->uid,
	'name' => (string) $user->name,
	'type' => 'music_album',
	'title' => utf8_encode($rdata[1]),
	'format' => NULL,
	'status' => true,
	'promote' => true,
	'sticky' => false,
	'created' => time(),
	'revision' => false,
	'comment' => '0',
	'field_album_artist' => array(
	array(
	'nid' => $nid
	)),
	);

	$node = (object) $node;
	module_load_include('inc', 'node', 'node.pages');
	
	$form_state = array();
	
	$form_state['values']['name'] = $node->name;
	$form_state['values']['op'] = t('Enregistrer');
	$form_state['status'] = 0;
	
	$form_state['values']['field_album_image'] = array(
	drupal_add_existing_file($file_drupal_path)
	);
	
	
	$a = drupal_execute('music_album_node_form',$form_state, $node);
	$errors = form_get_errors();
	if (count($errors)) {
		dsm($errors);
	}
}

Thanks a lot.

Comments

quicksketch’s picture

You really only need to set the "fid" value on your file fields, and they should be set an array, not an object. My guess is that the FID isn't getting saved into the CCK tables. I'd also generally recommend you use node_save() over drupal_execute() (for speed and simplicity). Use devel module to check the state of nodes when they're loaded and just make a pseudo-node object that has the exact same structure.

XmhO’s picture

Hi, thanks for answering.

Do you mean I should set a "fid" value? And I shouldn't convert node array to node object and pass it to node_save?

Could you give an example please? I think I didn't understand the "fid" setting concept...

Thanks :-)

quicksketch’s picture

I'll just refer you to the handbook page instead. I don't provide support on writing custom code in the issue queue. http://drupal.org/node/330421

XmhO’s picture

Hi, and thanks,

yeah I followed the instructions, and I'm able to add the nodes, but I still have one of the problems I had with filefield: When I'm adding for instance images, it's ok it works, but when I want to add some songs, it doesn't work, i got a filesize() stat failed... Whereas permissions are good and the file exists.

XmhO’s picture

Status: Active » Fixed

Solved. You were right quicksketch, It was about the fid that was not set.

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.