What is the best way to do this?
What is the proper way to remove the field in CCK and FILEFIELD module?? If I have a node is the best way $node->field_file_attach[0] = NULL or do I need to delete from content_field_file_attach and do a file_delete() ?
It appears so... Is this right? I really don't want to do the db_query and file_delete() bu don't want stuff left over.
$node = node_load($item->nid);
$count = db_result(db_query('SELECT count(*) from {content_field_file_attach} where field_file_attach_fid = %d',
$node->field_file_attach[0]['fid']));
if ($count) {
print "Found the file : $count\n";
db_query("UPDATE {content_field_file_attach} SET field_file_attach_fid = NULL,
field_file_attach_list = NULL, field_file_attach_data = NULL WHERE field_file_attach_fid = %d", $node->field_file_attach[0]['fid']);
}
file_delete($node->field_file_attach[0]['filepath']);
$node->field_file_attach[0] = NULL;
$node->status = 1;
$node->promote = 0; // Display on front page?
$node->sticky = 0; // Display top of page?
$node->format = 1; // Filtered HTML?
$node->validated = 1;
node_save($node);
Comments
Comment #1
quicksketchClose. What you have will work except that you should use the field_file_delete() function provided by FileField instead of file_delete(). file_delete() will delete the file itself but leave the database entry. field_file_delete() will check that the file is not used anywhere else, and if it isn't then it will delete both the file and the database entry in the files table.
Comment #2
billnbell CreditAttribution: billnbell commentedDo I need:
$node->field_file_attach[0] = NULL;
and node_save?
Also,
Replace file_delete($node->field_file_attach[0]['filepath']);
with the following?
field_file_delete($node->field_file_attach[0]);
Comment #3
billnbell CreditAttribution: billnbell commentedMaybe it should be:
field_file_delete($node->field_file_attach);
and $node->field_file_attach = NULL;
I have no idea.
Comment #4
quicksketchThe code you've used sort of is mixing approaches actually. You can either do it all manually (by calling field_file_delete() and deleting the row in content_field_file_attach), or you can do it through the node API, which would probably be the easier approach.
This should work, though I haven't tested it:
Comment #5
quicksketchComment #7
El Bandito CreditAttribution: El Bandito commentedSorry to re-open this one but I'm unclear. Quicksketches code in #4 seems to imply that if I set the $node's filefield to NULL and then node_save the file itself will be deleted ?
Surely the slight correction of billnbell's suggestion in #3 is the correct solution .... or not ?
The call to field_file_delete() removes the database record from the files table and the actual file itself, and NULLing the node field removes the now-defunct field.
Any pointers ?
Thanks
Comment #8
quicksketchYep that's right. You don't need to delete the file or the record from the files table, that's all done for you.
Comment #9
El Bandito CreditAttribution: El Bandito commentedThanks for the clarification and your efforts on this and other modules.
Issue closed.