[support] Fwd: Getting fid for saved file

Steve Edwards killshot91 at gmail.com
Wed Sep 21 20:25:33 UTC 2011


Sigh.  It's amazing how writing a message to a HUGE support list prompts you to look at things missed and fix your own problem.

In my case, I went back and looked at the documentation for hook_migrate_complete_{destination} and realized that the node was already saved.

  "The complete hook is called after successfully saving the migrated object - this allows other modules to perform operations that require the object to already exist (and have an ID), such as save data related to that object."

In other words, the node isn't going to be saved again after this hook.  However,  the node is saved after the prepare hook.  I had initially tried hook_migrate_prepare_node(), but it hadn't worked there, probably because of bugs I fixed after moving it to the complete hook.  I moved my code back to the prepare hook, and it works like a charm.

Remember boys and girls, always read the documentation carefully...

Steve

Begin forwarded message:

> From: Steve Edwards <killshot91 at gmail.com>
> Date: September 21, 2011 1:00:32 PM PDT
> To: support at drupal.org
> Subject: Getting fid for saved file
> 
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20110921/1fc80bcb/attachment.html 


More information about the support mailing list