[support] Getting fid for saved file
Steve Edwards
killshot91 at gmail.com
Wed Sep 21 20:00:32 UTC 2011
I'm using Migrate module to import and update nodes with data from external sources, and part of the process involves getting an image from a remote URL and saving it to an imagefield(uniquely named field_image) in the imported node. In my case, I'm using the hook_migrate_complete_node() hook to get the image and update the $node object. I've borrowed from Matt Butcher's post (http://technosophos.com/content/simple-pattern-importing-images) to get the following code:
$image_path = $url = $row->ridethis_combined_image;
$images_dir = file_directory_path().'/products/';
$image_path = substr($image_path, strpos($image_path, "/")+1);
$image_name = substr($image_path, strrpos($image_path, "/")+1);
$returned_image = drupal_http_request($url);
if (file_save_data($returned_image->data, $images_dir.$image_name, FILE_EXISTS_REPLACE)) {
$asset = new stdClass();
$asset->filename = $image_name;
$asset->uid = $node->uid;
$asset->nid = $node->nid;
$asset->filepath = $images_dir.$image_name;
$asset->filemime = file_get_mimetype($asset->filename);
$asset->timestamp = time();
$asset->status = 1;
drupal_write_record('files', $asset);
$node->field_image[] = (array)$asset;
//node_save($node);
}
The image is downloaded to the appropriate directory (sites/default/files/products) and the record is correctly saved in the files table. I put a breakpoint in my code and checked $node->field_image, and the $asset data is there. However, the image is not being displayed in the node. Upon inspection of the records in content_type_product, I (finally) noticed that the field_image_fid field was empty for each record. When I manually entered the appropriate fid from the files table, the image shows up fine when viewing/editing the node, and when displayed in a view.
So, after all that explanation, the question is, how do I get the fid once the record has been saved to the files table? Per the docs for drupal_write_record():
The $object parameter contains values for any serial fields defined by the $table. For example, $object->nid will be populated after inserting a new node.
which means that after the call to drupal_write_record(), the $asset object contains the fid (as $asset->fid) when it is put into $node->field_image[] (I verified this by examining the $asset object with a breakpoint in a debugger prior to adding it to $node, and also verified that $node->field_image[]['fid'] was populated after that).
The interesting thing about this is that if I use the exact same code in a custom action, it works fine. The only difference is that I do a node_save() right after adding the data to $object->field_image, where in the migrate hook I'm relying on migrate module to save the node. Based on that, I'm leaning towards the problem being with Migrate, but I wanted to check to see if there's something obvious I might be missing.
Thanks.
Steve
More information about the support
mailing list