? imagefield_338087.patch
Index: image_field.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/image_field.inc,v
retrieving revision 1.1
diff -u -p -r1.1 image_field.inc
--- image_field.inc	8 Feb 2009 06:31:25 -0000	1.1
+++ image_field.inc	8 Feb 2009 06:32:45 -0000
@@ -7,46 +7,16 @@
 
 
 /**
- * Implementation of hook_field_settings().
+ * Implementation of filefield's hook_field_settings().
  */
 
 function imagefield_image_field_settings_form($field) {
   $form = array();
-  $form['default'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Default image'),
-    '#element_validate' => array('_imagefield_field_settings_default_validate'),
-  );
-      // Present a thumbnail of the current default image.
-  $form['default']['use_default_image'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Use default image'),
-    '#default_value' =>  $field['use_default_image'],
-    '#description' => t('Check here if you want to use a image as default.'),
-  );
-  if (!empty($field['default_image'])) {
-    $form['default']['default_image_thumbnail'] = array(
-      '#type' => 'markup',
-      '#value' => theme('imagefield_image', $field['default_image'], '', '', array('width' => '150'), false),
-    );
-  }
-  $form['default']['default_image_upload'] = array(
-    '#type'  => 'file',
-    '#title' => t('Upload image'),
-    '#description' => t('Choose a image that will be used as default.'),
-  );
-
-  // We set this value on 'validate' so we can get cck to add it
-  // as a standard field setting.
-  $form['default_image'] = array(
-    '#type' => 'value',
-    '#value' => $field['default_image'],
-  );
   return $form;
 }
 
 function imagefield_image_field_settings_save($field) {
-  return array('default_image', 'use_default_image');
+  return array();
 }
 
 function imagefield_image_field_settings_database_columns($field) {
@@ -56,6 +26,7 @@ function imagefield_image_field_settings
   );
 }
 
+# TODO: THIS SEEMS LIKE SOME LEFT OVER CRAP FROM VIEWS 1. CAN WE DELETE IT?
 function imagefield_image_field_settings_filters($field) {
   return array(
     'not null' => array(
@@ -67,39 +38,7 @@ function imagefield_image_field_settings
  );
 }
 
-/**
- * Element specific validation for imagefield default value.
- *
- * Validated in a separate function from imagefield_field() to get access
- * to the $form_state variable.
- */
-function _imagefield_field_settings_default_validate($element, &$form_state) {
-  // Verify the destination exists
-  $dst = file_directory_path() .'/imagefield_default_images';
-  if (!field_file_check_directory($dst, FILE_CREATE_DIRECTORY)) {
-    form_set_error('default_image', t("The default image could not be uploaded. The destination(%d) does not exist or is not writable by the webserver.", array('%d' => dirname($dst))));
-    return;
-  }
-
-  $validators = array(
-    'file_validate_is_image' => array(),
-  );
-
-  // We save the upload here because we can't know the correct path until the file is saved.
-  if (!$file = file_save_upload('default_image_upload', $validators, $dst)) {
-    // no upload to save we hope... or file_save_upload reported an error on its own.
-    return;
-  }
-
-  // set new value.
-  $form_state['values']['default_image'] = (array)$file;
-
-  // remove old image & clean up database.
-  if (file_delete(file_create_path($field['default_image']['filepath']))) {
-    db_query('DELETE FROM {files} WHERE fid=%d', $field['default_image']['fid']);
-  }
-}
-
+# TODO: THIS SEEMS LIKE SOME LEFT OVER CRAP FROM VIEWS 1. CAN WE DELETE IT?
 /**
  * Custom filter for imagefield NOT null.
  */
Index: imagefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.74
diff -u -p -r1.74 imagefield.module
--- imagefield.module	5 Feb 2009 02:07:43 -0000	1.74
+++ imagefield.module	8 Feb 2009 06:32:45 -0000
@@ -154,7 +154,36 @@ function imagefield_field_settings($op, 
  * @see: function imagefield_field_settings().
  */
 function imagefield_field($op, $node, $field, &$items, $teaser, $page) {
-  return filefield_field($op, $node, $field, $items, $teaser, $page);
+  // Critical, we override the sanitize op of filefield_field to optionally
+  // display default images in nodes and views.
+  if ($op == 'sanitize') {
+    $default = _imagefield_widget_default_image($field);
+    if (empty($items)) {
+      $items[] = $default;
+    }
+    foreach ($items as $delta => $item) {
+      // Cleanup $items during node preview.
+      if (empty($item['fid']) || !empty($item['delete'])) {
+        // do only if no default value has been found.
+        if (!$default['fid']) { 
+          unset($items[$delta]);
+          continue;
+        }
+        else {
+          $items[$delta] = $default;
+        }
+      }
+      // Load the complete file if a filepath is not available.
+      if (!empty($item['fid']) && empty($item['filepath'])) {
+        $items[$delta] = array_merge($item, field_file_load($item['fid']));
+      }
+      // Add nid so formatters can create a link to the node.
+      $items[$delta]['nid'] = $node->nid;
+    }
+  }
+  else {
+    return filefield_field($op, $node, $field, $items, $teaser, $page);
+  }
 }
 
 /**
@@ -203,9 +232,12 @@ function imagefield_widget_settings($op,
 function imagefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
   // @todo: use CCK's default value callback.
   // add default values to items.
-  if (empty($items[$delta])) {
-    $items[$delta] = array('alt' => '', 'title' => '');
-  }
+  //if (empty($items[$delta])) {
+  //  $items[$delta] = array('alt' => '', 'title' => '');
+  //}
+  // NOTE: doing default value here is bad, since its stored to the database then
+  // which results in a fixed value, even if we assign a new default value for that field
+
   $element = filefield_widget($form, $form_state, $field, $items, $delta);
 
   $element['#upload_validators'] += imagefield_widget_upload_validators($field);
@@ -334,16 +366,14 @@ function theme_imagefield_admin_thumbnai
 /**
  * A few miscellansous functions in need of a proper home.
  */
-// CCK's default value callback... doesn't seem to work. I need to figure out what is going on in D6.
-function imagefield_default_value(&$form, &$form_state, $field, $delta) {
-  $items = filefield_default_value($form, $form_state, $field, $delta);
-  foreach($items as $delta => $item) {
-    $items[$delta]['data']['title'] = $field['widget']['title'];
-    $items[$delta]['data']['alt'] = $field['widget']['alt'];
-  }
-  return $items;
+//what about this default stuff instead?
+function _imagefield_widget_default_image($field) {
+  return isset($field['widget']['default_image']['fid']) && $field['widget']['use_default_image'] 
+    ? $field['widget']['default_image'] 
+    : array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => ''));
 }
 
+
 // Scale imagefield uploads.
 function _imagefield_scale_image($file, $resolution = 0) {
   $info = image_get_info($file['filepath']);
Index: imagefield_formatter.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield_formatter.inc,v
retrieving revision 1.7
diff -u -p -r1.7 imagefield_formatter.inc
--- imagefield_formatter.inc	28 Nov 2008 22:27:31 -0000	1.7
+++ imagefield_formatter.inc	8 Feb 2009 06:32:45 -0000
@@ -5,84 +5,89 @@
  * ImageField formatter hooks and callbacks.
  */
 
-
-function theme_imagefield_formatter_image_plain($element) {
-  // Inside a View this function may be called with null data.  In that case,
-  // just return.
-  if (empty($element['#item'])) {
-    return '';
+/**
+ * @param $element
+ *   The CCK element array. Its #item property will be populated with a file.
+ * @param $field
+ *   The CCK field array from content_fields($element['#field_name'], $element['#node']->type);
+ * @return A boolean indicating if there's data to format.
+ */
+function imagefield_formatter_prepare_item(&$element, $field) {
+  // If there is no image on the database, use default.
+  if (empty($element['#item']['fid']) && $field['widget']['default_image']) {
+    $element['#item'] = $field['widget']['default_image'];
+  }
+  // Load the file.
+  if (empty($element['#item']['filepath'])) {
+    $element['#item'] = array_merge($item, field_file_load($item['fid']));
   }
+  // Make sure that if we have any data...
+  if (empty($element['#item']['data'])) {
+    $element['#item']['data'] = array();
+  }
+  // ... that it's unserialized.
+  else if (is_string($element['#item']['data'])) {
+    $element['#item']['data'] = unserialize($element['#item']['data']);
+  }
+  return !empty($element['#item']);
+}
 
-  $field = content_fields($element['#field_name']);
+/**
+ * Create an <img> string for a field.
+ *
+ * @param $element
+ *   The CCK element array.
+ * @param $field
+ *   The CCK field array from content_fields($element['#field_name']);
+ * @return
+ *   An HTML image string.
+ */
+function imagefield_formatter_get_img($element, $field) {
   $item = $element['#item'];
-
-  if (empty($item['fid']) && $field['use_default_image']) $item = $field['default_image'];
-  if (empty($item['filepath']))  $item = array_merge($item, field_file_load($item['fid']));
-
   $class = 'imagefield imagefield-'. $field['field_name'];
-  return  theme('imagefield_image', $item, $item['data']['alt'], $item['data']['title'], array('class' => $class));
+  return theme('imagefield_image', $item, $item['data']['alt'], $item['data']['title'], array('class' => $class));
 }
 
-function theme_imagefield_formatter_image_nodelink($element) {
-  // Inside a View this function may be called with null data.  In that case,
-  // just return.
-  if (empty($element['#item'])) {
-    return '';
+function theme_imagefield_formatter_image_plain($element) {
+  $field = content_fields($element['#field_name'], $element['#node']->type);
+  if (imagefield_formatter_prepare_item($element, $field)) {
+    return imagefield_formatter_get_img($element, $field);
   }
+}
 
-  $node = $element['#node'];
-  $imagetag = theme('imagefield_formatter_image_plain', $element);
-  $class = 'imagefield imagefield-nodelink imagefield-'. $element['#field_name'];
-  return l($imagetag, 'node/'. $node->nid, array('attributes' => array('class' => $class), 'html' => true));
+function theme_imagefield_formatter_image_nodelink($element) {
+  $field = content_fields($element['#field_name'], $element['#node']->type);
+  if (imagefield_formatter_prepare_item($element, $field)) {
+    $imagetag = imagefield_formatter_get_img($element, $field);
+    $class = 'imagefield imagefield-nodelink imagefield-'. $element['#field_name'];
+    return l($imagetag, 'node/'. $element['#node']->nid, array('attributes' => array('class' => $class), 'html' => true));
+  }
 }
 
 function theme_imagefield_formatter_image_imagelink($element) {
-  // Inside a View this function may be called with null data.  In that case,
-  // just return.
-  if (empty($element['#item'])) {
-    return '';
+  $field = content_fields($element['#field_name'], $element['#node']->type);
+  if (imagefield_formatter_prepare_item($element, $field)) {
+    $imagetag = imagefield_formatter_get_img($element, $field);
+    $original_image_url = file_create_url($element['#item']['filepath']);
+    $attributes['class'] .= 'imagefield imagefield-imagelink imagefield-'. $element['#field_name'];
+    return l($imagetag, $original_image_url, array('attributes' => $attributes, 'html' => true));
   }
-
-  $item = $element['#item'];
-  $imagetag = theme('imagefield_formatter_image_plain', $element);
-  $original_image_url = file_create_url($item['filepath']);
-  $class = 'imagefield imagefield-imagelink imagefield-'. $element['#field_name'];
-  return l($imagetag, $original_image_url, array('attributes' => array('class' => $class), 'html' => true));
 }
 
 function theme_imagefield_formatter_path_plain($element) {
-  // Inside a View this function may be called with null data.  In that case,
-  // just return.
-  if (empty($element['#item'])) {
-    return '';
+  $field = content_fields($element['#field_name'], $element['#node']->type);
+  if (imagefield_formatter_prepare_item($element, $field)) {
+    $original_image_url = file_create_url($element['#item']['filepath']);
+    $attributes['class'] .= ' imagefield-formatter-path';
+    return '<span '. drupal_attributes($attributes) .'>'. $original_image_url .'</span>';
   }
-
-  $field = content_fields($element['#field_name']);
-  $item = $element['#item'];
-  if (empty($item['fid']) && $field['use_default_image'])  $item = $field['default_image'];
-  // If there is no image on the database, use default.
-  if (empty($item['filepath']))  $item = array_merge($item, field_file_load($item['fid']));
-
-  $attributes['class'] .= ' imagefield-formatter-path';
-  return '<span '. drupal_attributes($attributes) .'>'. file_create_path($item['filepath']) .'</span>';
 }
 
 function theme_imagefield_formatter_url_plain($element) {
-  // Inside a View this function may be called with null data.  In that case,
-  // just return.
-  if (empty($element['#item'])) {
-    return '';
+  $field = content_fields($element['#field_name'], $element['#node']->type);
+  if (imagefield_formatter_prepare_item($element, $field)) {
+    $attributes['class'] .= ' imagefield-formatter-url';
+    return '<span '. drupal_attributes($attributes) .'>'. file_create_url($item['filepath']) .'</span>';
   }
-
-  $field = content_fields($element['#field_name']);
-  $item = $element['#item'];
-  if (empty($item['fid']) && $field['use_default_image'])  $item = $field['default_image'];
-  // If there is no image on the database, use default.
-  if (empty($item['filepath']))  $item = array_merge($item, field_file_load($item['fid']));
-
-  $attributes['class'] .= ' imagefield-formatter-url';
-  return '<span '. drupal_attributes($attributes) .'>'. file_create_url($item['filepath']) .'</span>';
 }
 
-
-
Index: imagefield_widget.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield_widget.inc,v
retrieving revision 1.21
diff -u -p -r1.21 imagefield_widget.inc
--- imagefield_widget.inc	27 Aug 2008 14:53:56 -0000	1.21
+++ imagefield_widget.inc	8 Feb 2009 06:32:45 -0000
@@ -95,12 +95,85 @@ function imagefield_widget_widget_settin
     '#description' => t('This value will be used as the image title by default.'),
     '#suffix' =>  theme('token_help', 'file'),
   );
+  
+  
+  // Default image settings.
+  $form['default'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Default image'),
+    '#element_validate' => array('_imagefield_widget_settings_default_validate'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#weight' => 10
+  );
+  
+  // Present a thumbnail of the current default image.
+  $form['default']['use_default_image'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use default image'),
+    '#default_value' =>  $widget['use_default_image'],
+    '#description' => t('Check here if you want to use a image as default.'),
+  );
+  
+  if (!empty($widget['default_image'])) {
+    $form['default']['default_image_thumbnail'] = array(
+      '#type' => 'markup',
+      '#value' => theme('imagefield_image', $widget['default_image'], '', '', array('width' => '150'), false),
+    );
+  }
+  $form['default']['default_image_upload'] = array(
+    '#type'  => 'file',
+    '#title' => t('Upload image'),
+    '#description' => t('Choose a image that will be used as default.'),
+  );
+  
+  // We set this value on 'validate' so we can get cck to add it
+  // as a standard field setting.
+  $form['default_image'] = array(
+    '#type' => 'value',
+    '#value' => $widget['default_image'],
+  );
+
   return $form;
 }
 
 function imagefield_widget_widget_settings_save($widget) {
   //@todo: rename custom_alt and custom_title to alt_custom and title_custom to be OCD.
-  return array('max_resolution', 'min_resolution', 'alt',  'custom_alt', 'title', 'custom_title');
+  return array('max_resolution', 'min_resolution', 'alt',  'custom_alt', 'title', 'custom_title', 'default_image', 'use_default_image');
+}
+
+/**
+ * Element specific validation for imagefield default value.
+ *
+ * Validated in a separate function from imagefield_field() to get access
+ * to the $form_state variable.
+ */
+function _imagefield_widget_settings_default_validate($element, &$form_state) {
+  // Verify the destination exists
+  $dst = file_directory_path() .'/imagefield_default_images';
+  if (!field_file_check_directory($dst, FILE_CREATE_DIRECTORY)) {
+    form_set_error('default_image', t("The default image could not be uploaded. The destination %dest does not exist or is not writable by the webserver.", array('%dest' => dirname($dst))));
+    return;
+  }
+
+  $validators = array(
+    'file_validate_is_image' => array(),
+  );
+
+  // We save the upload here because we can't know the correct path until the file is saved.
+  if (!$file = file_save_upload('default_image_upload', $validators, $dst)) {
+    // No upload to save we hope... or file_save_upload() reported an error on its own.
+    return;
+  }
+
+  // Make the file permanent and store it in the form.
+  file_set_status($file, FILE_STATUS_PERMANENT);
+  $form_state['values']['default_image'] = (array)$file;
+
+  // Remove old image & clean up database.
+  if (file_delete(file_create_path($field['default_image']['filepath']))) {
+    db_query('DELETE FROM {files} WHERE fid=%d', $field['default_image']['fid']);
+  }
 }
 
 /**
@@ -122,9 +195,11 @@ function imagefield_widget_widget_value(
       return array(
         'alt' => isset($edit['alt']) ? $edit['alt'] : '',
         'title' => isset($edit['title']) ? $edit['title'] : '',
+        'default_image' => isset($edit['default_image']) ? $edit['default_image'] : '',
+        'use_default_image' => isset($edit['use_default_image']) ? $edit['use_default_image'] : '',
       );
   }
-  return array('alt' => '', 'title' => '');
+  return array('alt' => '', 'title' => '', 'default_image' => '', 'use_default_image' => '');
 }
 
 function imagefield_widget_widget_process($element, $edit, &$form_state, $form) {
@@ -167,5 +242,3 @@ function theme_imagefield_widget(&$eleme
   return theme('form_element', $element, $element['#children']);
 }
 
-
-
