简体   繁体   中英

Drupal7 copy filefield to another node at submit

I have a content type that has unlimited filefield fields in addition to other fields. At node save/submit I would like to create an additional node for each file in the field, and assign that filefield to it. I'm fine with the nodeapi hooks and progmatically creating the node, but I can't access the content of the filefield from the node. When I print the filefield contents from within hook_node_insert I get: ...

 (
    [fid] => 38
    [display] => 1
    [description] => 
    [upload_button] => Upload
    [remove_button] => Remove
    [upload] => 
    )

....

Not the formatted and proceed field I would normally see. My suspicion is that I can access this somehow from the form and do a form submit after modifying it, but I'm not sure how to do this and it may not the best way. Let me know if you have any tips on this, greatly appreciated.

since we have [fid] populated we can use file_load($fid) to load the file object. Then you can cast this file object to array and then attach it to the file field of newly created node.

Loop through the array and for each $fid you encounter..

$file = file_load($fid);    
$new_node= new StdClass();
$new_node->type = 'image';
$new_node->language = LANGUAGE_NONE;
node_object_prepare($new_node);

// add additional data about new node.

$new_node->field_custom_files[LANGUAGE_NONE][] = array($file);

node_submit($new_node);
node_save($new_node);

I have not tested this, but do let me know if you face any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM