I am facing a situation here.
I have a content type A with an image field(field_upload_snaps) in it. There is another content type B which also contains an image field(field_valid_snaps).
Now user will upload the pics from A content type. So if user upload 3 pics via A then 3 FID and their corresponding URIs will be created in file_managed table in Drupal 7.
Now I have a requirement where I have to insert 2 images out of 3 programatically using Drupal code. Now there is no need to generate any new image copy in public URI as images already exist via A.
I tried two approaches:-
1) I have created a new node of Type B. I tried to fetch file URI,filesize,filemime and all relevant parameter from existing fid and associate with the node using this.
global $user; $node = new stdClass(); $node->type = "user_slideshow_snaps"; $node->language = LANGUAGE_NONE; node_object_prepare($node); $node->uid = $user->uid; $file = (object) array( 'uid' => $user->uid, 'uri' => 'public://1.jpeg" , 'filemime' => 'image/jpeg', 'status' => 1, ); $node->field_upload_snaps['und'] = (array)$file; $node = node_submit($node); // Prepare node for a submit node_save($node);
But it gives me an error that "Cant insert duplicate value in file_managed". It will give because such value already exist in file_managed via A. So I am not able to save value.
2) I saved node first and then manually insert in the image field like this.
global $user; $node = new stdClass(); $node->type = "user_slideshow_snaps"; $node->language = LANGUAGE_NONE; node_object_prepare($node); $node->uid = $user->uid; $node = node_submit($node); // Prepare node for a submit node_save($node); db_insert('field_data_field_valid_snaps') ->fields(array( 'entity_type' => 'node', 'bundle' => 'user_actualD_snaps', 'deleted'=> 0, 'entity_id'=>$node->nid, 'delta'=>0, 'field_valid_snaps_fid'=>517, 'field_valid_snaps_alt'=>'', 'field_valid_snaps_title'=>'', 'field_valid_snaps_width'=>200, 'field_valid_snaps_height'=>300, )) ->execute();
But the line after node_save() doesnt execute in this case. So I am not able to save it.
Are there any functions available which will map existing images into another table?