Index: imagefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.81
diff -u -r1.81 imagefield.module
--- imagefield.module	11 Mar 2009 06:43:22 -0000	1.81
+++ imagefield.module	18 Mar 2009 03:43:55 -0000
@@ -80,6 +80,10 @@
       'arguments' => array('element' => null),
       'file' => 'imagefield_formatter.inc',
     ),
+    'imagefield_formatter_image_customlink' => array(
+      'arguments' => array('element' => null),
+      'file' => 'imagefield_formatter.inc',
+    ),
   );
 }
 
@@ -225,6 +229,12 @@
       'css' => array($module_path .'/imagefield.css'),
       'description' => t('Displays image files in their original size.'),
     ),
+    'image_customlink' => array(
+      'label' => t('Image linked to custom URL'),
+      'field types' => array('filefield'),
+      'css' => array($module_path .'/imagefield.css'),
+      'description' => t('Displays image files in their original size linked to a custom URL.'),
+    ),
   );
   return $formatters;
 }
Index: imagefield_formatter.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield_formatter.inc,v
retrieving revision 1.9
diff -u -r1.9 imagefield_formatter.inc
--- imagefield_formatter.inc	9 Mar 2009 02:47:20 -0000	1.9
+++ imagefield_formatter.inc	18 Mar 2009 03:39:30 -0000
@@ -91,3 +91,21 @@
   $attributes['class'] .= ' imagefield-formatter-url';
   return '<span '. drupal_attributes($attributes) .'>'. file_create_url($item['filepath']) .'</span>';
 }
+
+function theme_imagefield_formatter_image_customlink($element) {
+  // Inside a View this function may be called with null data. In that case,
+  // just return.
+  if (empty($element['#item'])) {
+    return '';
+  }
+
+  $item = $element['#item'];
+  $imagetag = theme('imagefield_formatter_image_plain', $element);
+  if (!empty($item['data']['link'])) {
+    $class = 'imagefield imagefield-customlink imagefield-'. $element['#field_name'];
+    return l($imagetag, $item['data']['link'], array('attributes' => array('class' => $class), 'html' => true));
+  }
+  else {
+    return $imagetag;
+  }
+}
\ No newline at end of file
Index: imagefield_widget.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield_widget.inc,v
retrieving revision 1.29
diff -u -r1.29 imagefield_widget.inc
--- imagefield_widget.inc	17 Mar 2009 00:57:55 -0000	1.29
+++ imagefield_widget.inc	18 Mar 2009 03:44:03 -0000
@@ -92,6 +92,28 @@
     '#description' => t('This value will be used as the image title by default.'),
     '#suffix' =>  theme('token_help', 'file'),
   );
+
+  $form['link_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Link settings'),
+    '#collapsible' => true,
+    '#collapsed' => true,
+    '#weight' => 9,
+  );
+  $form['link_settings']['custom_link'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable custom URL'),
+    '#default_value' =>  !empty($widget['custom_link']) ? $widget['custom_link'] : 0,
+    '#description' => t('Enable user input of URL for images.'),
+  );
+  $form['link_settings']['link'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Default URL'),
+    '#default_value' => !empty($widget['link']) ? $widget['link'] : '',
+    '#description' => t('This value will be used as the link URL by default.'),
+    '#suffix' =>  theme('token_help', 'file'),
+  );
+
   return $form;
 }
 
@@ -120,7 +142,7 @@
 function imagefield_widget_settings_save($widget) {
   // TODO: Rename custom_alt and custom_title to alt_custom and title_custom.
   $filefield_settings = module_invoke('filefield', 'widget_settings', 'save', $widget);
-  return array_merge($filefield_settings, array('max_resolution', 'min_resolution', 'alt',  'custom_alt', 'title', 'custom_title'));
+  return array_merge($filefield_settings, array('max_resolution', 'min_resolution', 'alt',  'custom_alt', 'title', 'custom_title', 'link', 'custom_link',));
 }
 
 /**
@@ -131,10 +153,12 @@
   if ($edit) {
     $item['alt'] = isset($edit['alt']) ? $edit['alt'] : '';
     $item['title'] = isset($edit['title']) ? $edit['title'] : '';
+    $item['link'] = isset($edit['link']) ? $edit['link'] : '';
   }
   else {
     $item['alt'] = '';
     $item['title'] = '';
+    $item['link'] = '';
   }
   return $item;
 }
@@ -187,6 +211,23 @@
     $element['data']['title']['#value'] = $field['widget']['title'];
   }
 
+  // Check if using the default link URL and replace tokens.
+  $default_link = (!$field['widget']['custom_link'] || ($file['status'] == 0 && empty($file['data']['link'])));
+  if ($default_link && function_exists('token_replace')) {
+    $field['widget']['link'] = token_replace($field['widget']['link'], 'user', $GLOBALS['user']);
+  }
+  $element['data']['link'] = array(
+    '#type' => $field['widget']['custom_link'] ? 'textfield' : 'value',
+    '#title' => t('Link to'),
+    '#default_value' => $default_link ? $field['widget']['link'] : $file['data']['link'],
+    '#description' => t('The URL that you would like the image to link to.'),
+    '#attributes' => array('class' => 'imagefield-text'),
+  );
+  // #value must be hard-coded if #type = 'value'.
+  if ($default_link) {
+    $element['data']['link']['#value'] = $field['widget']['link'];
+  }
+
   return $element;
 }
 

