--- ./modules/imagefield/imagefield.install	Tue Nov 11 17:51:04 2008
+++ ./modules/imagefield/imagefield.install	Tue Nov 11 17:53:22 2008
@@ -89,7 +89,6 @@
 
 /**
  * Upgrade to CCK 2 and Drupal 6.
- * @todo: update widget type from 'image' to 'imagefield_widget_default'
  * @todo: update formatter name changes.
  *    default => image_plain
  *    imagefield_nodelink => image_nodelink
@@ -236,4 +235,50 @@
   $context['finished'] = 1;
 }
 
+/**
+ * Transform 'image's to 'imagefield_widget's.
+ */
+function imagefield_update_6002() {
+  // Get default values for widget from widget's init form.
+  $form_template = array();
+  foreach (array('file' => 'filefield', 'image' => 'imagefield_widget') as $module => $field) {
+    include_once(drupal_get_path('module', $module.'field') .'/'.$module.'field_widget.inc');
+    $function = $field.'_widget_settings_form';
+    $field_form = $function(array()); // Pass an empty array to get defaults.
+    // Recursively go through form to find all fields having default values.
+    while (count($field_form)) {
+      // Work on first item, and remove it from the to do list.
+      $item_names = array_keys($field_form);
+      $item_name = $item_names[0];
+      $item = $field_form[$item_name];
+      unset($field_form[$item_name]);
+      // If this item has a default value, append it to our template form.
+      if (array_key_exists('#default_value', $item)) {
+        $form_template[$item_name] = $item['#default_value'];
+      }
+      // Append subforms to the list of forms to explore.
+      foreach ($item as $subitem_name => $subitem_value) {
+        if (is_array($subitem_value) && $subitem_name{0} != '#') {
+          $field_form[$subitem_name] = $subitem_value;
+        }
+      }
+    }
+  }
+  // Get unconverted widgets and transform them, using the form template to fill unspecified values.
+  module_load_include('inc', 'content', 'includes/content.crud'); // TODO: abort the update if this file cannot be included, alert about necessity to upgrade CCK.
+  foreach (content_field_instance_read(array('widget_type' => 'image'), TRUE) as $field) {
+    $field['widget'] = array_merge($form_template, $field['widget'], array('type' => 'imagefield_widget'));
+    // Transform some fields to work with filefield.
+    foreach (array('image_path' => 'file_path') as $old_attr => $new_attr) {
+      $field['widget'][$new_attr] = $field['widget'][$old_attr];
+      unset($field['widget'][$old_attr]);
+    }
+    // Write widget back.
+    _content_field_instance_write($field);
+  }
+  // Trigger CCK activations.
+  cache_clear_all('content_type_info', content_cache_tablename());
+  content_associate_fields('imagefield');
+  return array();
+}

