Posted by El Bandito on December 19, 2012 at 1:53pm
Hi
I am creating a replica of an existing node something like :
$new_node = clone $original_node;
unset($new_node->nid);
unset($new_node->vid);
unset($new_node->created);
nod_save_($new_node);The problem is that the node has a file attached via a file field. I want the cloned version to have its pwn replica of the attached file, rather than just reference the same uploaded file.
Any ideas would be appreciated.
Comments
Forget the above. I've had a
Forget the above. I've had a look at the file API and it seems file_copy looks like the correct function. This returns me a new file object representing a clone of the original file object. What I don't understand is how to correctly insert the new file object into my cloned node as the file object lacks 3 fields ( display, description and rdf_mapping ) that I can see are present in the file field.
Any suggestions ?
This forum is a lonely place
This forum is a lonely place ;-)
Here's my ( dodgy ? ) solution that works for me :
$original_node = node_load(85);
$new_node = clone $original_node;
unset($new_node->nid);
unset($new_node->vid);
$file_obj = file_load($new_node->field_test_file_attachment[$new_node->language][0]['fid']);
$file_obj_copy = file_copy($file_obj, 'public://attachments', 'FILE_EXISTS_RENAME');
if ($file_obj_copy) {
// add in some fields that seem to appear in the original field but not the cloned file object.
// no idea what they do but we'll play safe
$file_obj_copy->display = '1';
$file_obj_copy->rdf_mapping = array();
$file_obj_copy->description = '';
}
// casting the obj to an array works but some of the strings keys become integers however
// the node_save subsequently fixes this
$new_node->field_test_file_attachment[$new_node->language][0] = (array) $file_obj_copy;
node_save($new_node);