? .imagefield_formatter.inc.swp
? imagefield-6--3-0-ALPHA-2_0.patch
? yh_33.patch
Index: imagefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.71
diff -u -p -r1.71 imagefield.module
--- imagefield.module	27 Aug 2008 15:03:06 -0000	1.71
+++ imagefield.module	3 Feb 2009 22:37:50 -0000
@@ -135,12 +135,52 @@ function imagefield_field_settings($op, 
 }
 
 /**
+ * Implementation of hook_form_alter(). Set the appropriate
+ * attibutes to allow file uploads on the field settings form.
+ */
+function imagefield_form_alter(&$form, $form_state, $form_id) {
+  if ($form_id == 'content_field_edit_form') {
+    $form['#attributes'] = array('enctype' => 'multipart/form-data');
+  }
+}
+
+
+/**
  * Implementation of CCK's hook_field().
  *
  * @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);
+  }
 }
 
 /**
@@ -189,9 +229,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);
@@ -282,7 +325,7 @@ function theme_imagefield_image($file, $
     $attributes = drupal_attributes($attributes);
     $alt = empty($alt) ? $file['alt'] : $alt;
     $title = empty($title) ? $file['title'] : $title;
-    $url = file_create_url($file['filepath']);
+    $url = file_create_url($file['filepath']) . '?' . $file['timestamp'];
     return '<img src="'. check_url($url) .'" alt="'.
         check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
   }
@@ -320,16 +363,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	3 Feb 2009 22:37:50 -0000
@@ -5,84 +5,86 @@
  * 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 the data is unserialized.
+  if (is_string($element['#item']['data'])) {
+    $element['#item']['data'] = unserialize($element['#item']['data']);
+  }
+dsm($element);
+  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	3 Feb 2009 22:37:50 -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;
+  }
+
+  // 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']);
+  }
 }
 
 /**
@@ -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) {
