Index: imagefield.css =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.css,v retrieving revision 1.2.4.1 diff -u -p -r1.2.4.1 imagefield.css --- imagefield.css 17 Dec 2007 04:47:05 -0000 1.2.4.1 +++ imagefield.css 14 Apr 2008 19:18:56 -0000 @@ -1,14 +1,8 @@ -div.imagefield-edit-image-row { - border: 1px solid #e5e5e5; - padding: 5px; - margin-bottom: 10px; -} - div.imagefield-edit-preview img { border: 1px solid #e5e5e5; } -div.imagefield-edit-preview { +div.imagefield-edit-preview { float: left; } @@ -26,7 +20,9 @@ div.imagefield-edit-image-detail { } div.imagefield-edit-image-flags div.form-item { - float: right; + float: right; } - +div.imagefield-element input.form-submit { + margin: 0 0.5em 0 0; +} Index: imagefield.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.info,v retrieving revision 1.4.4.2 diff -u -p -r1.4.4.2 imagefield.info --- imagefield.info 6 Dec 2007 09:25:39 -0000 1.4.4.2 +++ imagefield.info 14 Apr 2008 19:18:56 -0000 @@ -1,5 +1,6 @@ ; $Id: imagefield.info,v 1.4.4.2 2007/12/06 09:25:39 dopry Exp $ name = Image Field description = Defines an image field type. -dependencies = content +core = 6.x +dependencies[] = content package = CCK Index: imagefield.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.install,v retrieving revision 1.2.4.1 diff -u -p -r1.2.4.1 imagefield.install --- imagefield.install 18 Apr 2007 21:02:19 -0000 1.2.4.1 +++ imagefield.install 14 Apr 2008 19:18:57 -0000 @@ -73,6 +73,22 @@ function imagefield_update_2() { return $ret; } +/** + * Upgrade to CCK 2 and Drupal 6. + */ +function imagefield_update_6100() { + include_once('./'. drupal_get_path('module', 'content') .'/content_crud.inc'); + $fields = content_fields(); + foreach ($fields as $field) { + // Convert max_number_images to CCK's multiple limit. + if ($field['type'] == 'image' && $field['widget']['max_number_images'] > 0) { + $field['multiple'] = $field['widget']['max_number_images']; + $field = array_merge($field, $field['widget']); + content_field_instance_update($field); + } + } + return array(); +} Index: imagefield.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v retrieving revision 1.30.2.6.2.49 diff -u -p -r1.30.2.6.2.49 imagefield.module --- imagefield.module 2 Apr 2008 16:06:00 -0000 1.30.2.6.2.49 +++ imagefield.module 14 Apr 2008 19:18:57 -0000 @@ -4,93 +4,30 @@ /** * @file * Defines an image field type. - * imagefield uses content.module to store the fid, and the drupal files + * imagefield uses content.module to store the fid, and the drupal files * table to store the actual file data. * */ - -function imagefield_menu($maycache) { +/** + * Implementation of hook_menu(). + */ +function imagefield_menu() { $items = array(); - if ($maycache) { - $items[] = array( - 'path' => 'imagefield/js', - 'callback' => 'imagefield_js', - //'access' => user_access(), - 'access' => TRUE, - 'type' => MENU_CALLBACK - ); - } - elseif ($_SESSION['imagefield']) { - // do this variable_get once.... - $download_method = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC); - - // Iterate over each field stored in session imagefield looking - // for files in a preview state to add menu items for. This - // allows us to preview new uploads before a node is submitted. - foreach ($_SESSION['imagefield'] as $fieldname => $files) { - // move on to the next field if there is nothing to process in files. - if (empty($files)) continue; - - foreach($files as $delta => $file) { - // If the file is not a preview do not display it. - if (empty($file['preview'])) continue; - - // filepath to pass into _imagefield_preview - $filepath = $file['preview']; - - // build url path for private files menu item. - if ($download_method == FILE_DOWNLOADS_PRIVATE) { - // strip out the file_directory_path. It shouldn't be a part - // of the URL with private downloads. - if (strpos($file['preview'], file_directory_path()) !== FALSE) { - $file['preview'] = trim(substr($file['preview'], strlen(file_directory_path())), '\\/'); - } - $file['preview'] = 'system/files/' . $file['preview']; - } - $items[] = array( - 'path' => $file['preview'], - 'callback' => '_imagefield_preview', - 'callback arguments' => array($filepath), - 'access' => TRUE, - 'type' => MENU_CALLBACK, - ); - } - } - } + $items['imagefield/js'] = array( + 'page callback' => 'imagefield_js', + 'access callback' => 'user_access', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; } /** - * transfer a file that is in a 'preview' state. - * @todo multiple support + * Implementation of hook_perm(). */ -function _imagefield_preview($filepath) { - foreach ($_SESSION['imagefield'] as $fieldname => $files) { - foreach ($files as $delta => $file) { - if ($file['preview'] == $filepath) { - // Emulate a normal file transfer by setting cache flags that will - // prevent the image from needing to be resent on previews. Without - // setting the cache headers, transfered images still get the expire - // headers set in drupal_page_header(). - $modified_time = filemtime($file['filepath']); - $last_modified = gmdate('D, d M Y H:i:s', $modified_time) .' GMT'; - $etag = '"'.md5($last_modified).'"'; - - file_transfer($file['filepath'], array( - 'Content-Type: '. mime_header_encode($file['filemime']), - 'Content-Length: '. $file['filesize'], - 'Cache-Control: max-age=1209600', - 'Expires: '. gmdate('D, d M Y H:i:s', time() + 1209600) .' GMT', // Two weeks. - 'Last-Modified: '. $last_modified, - 'ETag: '. $etag, - )); - } - } - } -} - function imagefield_perm() { return array('view imagefield uploads'); } @@ -100,7 +37,10 @@ function imagefield_perm() { */ function imagefield_field_info() { return array( - 'image' => array('label' => 'Image'), + 'image' => array( + 'label' => t('Image'), + 'description' => t('Store an image file and text for alt and title tags.'), + ), ); } @@ -109,14 +49,12 @@ function imagefield_field_info() { */ function imagefield_field_settings($op, $field) { switch ($op) { - case 'callbacks': - return array('view' => CONTENT_CALLBACK_CUSTOM); - case 'form': $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( @@ -144,24 +82,14 @@ function imagefield_field_settings($op, ); return $form; - case 'validate': - // We save the upload here because we can't know the correct - // file path until we save the file. - if ($file = file_check_upload('default_image_upload')) { - if ($file = file_save_upload('default_image_upload', file_directory_path())) { - form_set_value(array('#parents' => array('default_image')), (array) $file); - } - } - - break; case 'save': return array('default_image', 'use_default_image'); case 'database columns': $columns = array( 'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), - 'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), - 'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), + 'title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', 'sortable' => TRUE), + 'alt' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', 'sortable' => TRUE), ); return $columns; @@ -178,7 +106,20 @@ function imagefield_field_settings($op, } /** - * Custom filter for imagefield NOT NULL + * 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) { + // We save the upload here because we can't know the correct + // file path until we save the file. + if ($file = file_save_upload('default_image_upload', array(), file_directory_path())) { + $form_state['values']['default_image'] = (array)$file; + } +} + +/** + * Custom filter for imagefield NOT NULL. */ function imagefield_views_handler_filter_is_not_null($op, $filter, $filterinfo, &$query) { if ($op == 'handler') { @@ -194,72 +135,48 @@ function imagefield_views_handler_filter } -function imagefield_default_item() { - return array( - 'fid' => 0, - 'title' => '', - 'alt' => '', - ); -} - /** - * insert a file into the database. + * Make a temporary file permanent in the files table. + * * @param $node - * node object file will be associated with. + * Node object this file is be associated with. * @param $file - * file to be inserted, passed by reference since fid should be attached. - * + * File to be inserted, passed by reference since fid should be attached. */ function imagefield_file_insert($node, &$file, $field) { - $fieldname = $field['field_name']; - - // allow tokenized paths. - if (function_exists('token_replace')) { - global $user; - $widget_image_path = token_replace($field['widget']['image_path'],'user', $user); - } - else { - $widget_image_path = $field['widget']['image_path']; + if ($file['flags']['delete'] == TRUE) { + if (_imagefield_file_delete($file, $field['field_name'])) { + return array(); + } } - - $filepath = file_create_path($widget_image_path) . '/' . $file['filename']; - - if (imagefield_check_directory($widget_image_path) && $file = file_save_upload((object)$file, $filepath)) { + elseif (isset($file['fid']) && $file['fid'] > 0) { + $file = (object)$file; + file_set_status($file, FILE_STATUS_PERMANENT); $file = (array)$file; - $file['fid'] = db_next_id('{files}_fid'); - db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) - VALUES (%d, %d, '%s','%s','%s',%d)", - $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']); - module_invoke_all('imagefield_file', 'save', $file); - return (array)$file; - } - else { - // Include file name in upload error. - form_set_error(NULL, t('Image upload was unsuccessful.')); - return FALSE; + return $file; } } - /** - * update the file record if necessary + * Update the file record if necessary. + * * @param $node + * Node object this file is be associated with. * @param $file + * A single CCK image field item to be updated. * @param $field + * The field definition for this image field. */ function imagefield_file_update($node, &$file, $field) { - $file = (array)$file; + $file = (array)$file; if ($file['flags']['delete'] == TRUE) { - if(_imagefield_file_delete($file, $field['field_name'])) { + if (_imagefield_file_delete($file, $field['field_name'])) { return array(); } } - if ($file['fid'] == 'upload') { - return imagefield_file_insert($node, $file, $field); - } else { // if fid is not numeric here we should complain. - // else we update the file table. + // else we update the file table. } return $file; } @@ -296,8 +213,6 @@ function imagefield_field($op, $node, $f unset($items[$delta]); } } - $items = array_values($items); // compact deltas - imagefield_clear_field_session($fieldname); break; // called before content.module defaults. @@ -311,7 +226,7 @@ function imagefield_field($op, $node, $f $item['flags']['hidden'] = TRUE; $item['flags']['delete'] = TRUE; } - + // Update each file item. $items[$delta] = imagefield_file_update($node, $item, $field); @@ -324,30 +239,29 @@ function imagefield_field($op, $node, $f } } } - $items = array_values($items); // compact deltas - imagefield_clear_field_session($fieldname); break; case 'delete': foreach ($items as $delta => $item) { - _imagefield_file_delete($item, $field['field_name']); + _imagefield_file_delete($item, $field['field_name']); } break; - case 'view': - $context = $teaser ? 'teaser' : 'full'; - $formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default'; + case 'sanitize': + // Cleanup $items during node preview. + foreach ($items as $delta => $item) { + if (empty($item['fid']) || $item['flags']['delete']) { + unset($items[$delta]); + } + } + + // If completely empty, check for a default image. if ($field['use_default_image'] && empty($items) ) { $items[0] = $field['default_image']; } - foreach ($items as $delta => $item) { - $items[$delta]['view'] = content_format($field, $item, $formatter, $node); - } - return theme('field', $node, $field, $items, $teaser, $page); } } - /** * Implementation of hook_widget_info(). */ @@ -356,6 +270,8 @@ function imagefield_widget_info() { 'image' => array( 'label' => 'Image', 'field types' => array('image'), + 'multiple values' => CONTENT_HANDLE_MODULE, + 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), ), ); } @@ -365,52 +281,42 @@ function imagefield_widget_info() { */ function imagefield_widget_settings($op, $widget) { switch ($op) { - case 'callbacks': - return array('default value' => CONTENT_CALLBACK_CUSTOM); - case 'form': $form = array(); - $form['max_resolution'] = array ( - '#type' => 'textfield', - '#title' => t('Maximum resolution for Images'), + $form['max_resolution'] = array( + '#type' => 'textfield', + '#title' => t('Maximum resolution for Images'), '#default_value' => $widget['max_resolution'] ? $widget['max_resolution'] : 0, - '#size' => 15, - '#maxlength' => 10, - '#description' => + '#size' => 15, + '#maxlength' => 10, + '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height.'), ); - $form['max_filesize'] = array ( - '#type' => 'textfield', - '#title' => t('Maximum filesize for Images'), + $form['max_filesize'] = array( + '#type' => 'textfield', + '#title' => t('Maximum filesize for Images'), '#default_value' => $widget['max_filesize'] ? $widget['max_filesize'] : 0, - '#size' => 6, + '#size' => 6, '#description' => t('The maximum allowed image file size expressed in kilobytes. Set to 0 for no restriction.') ); - $form['max_number_images'] = array ( - '#type' => 'textfield', - '#title' => t('Maximum number of images'), - '#default_value' => $widget['max_number_images'] ? $widget['max_number_images'] : 0, - '#size' => 4, - '#description' => t('The maximum number of images allowed. Set to 0 for no restriction. This only applies if multiple values are enabled.') - ); $form['image_path'] = array( - '#type' => 'textfield', - '#title' => t('Image path'), - '#default_value' => $widget['image_path'] ? $widget['image_path'] : '', - '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))), + '#type' => 'textfield', + '#title' => t('Image path'), + '#default_value' => $widget['image_path'] ? $widget['image_path'] : '', + '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))), ); if (function_exists('token_replace')) { $form['image_path']['#description'] .= ' '. t('You can use the following tokens in the image path.'); $form['image_path']['#suffix'] = theme('token_help', 'user'); } - $form['file_extensions'] = array ( - '#type' => 'textfield', - '#title' => t('Permitted upload file extensions.'), + $form['file_extensions'] = array( + '#type' => 'textfield', + '#title' => t('Permitted upload file extensions.'), '#default_value' => $widget['file_extensions'] ? $widget['file_extensions'] : 'jpg jpeg png gif', - '#size' => 64, - '#maxlength' => 64, + '#size' => 64, + '#maxlength' => 64, '#description' => t('Extensions a user can upload to this field. Seperate extensions with a space and do not include the leading dot.') - ); + ); $form['custom_alt'] = array( '#type' => 'checkbox', @@ -427,13 +333,13 @@ function imagefield_widget_settings($op, return $form; case 'validate': - // strip slashes from the beginning and end of $widget['image_path'] - $widget['image_path'] = trim($widget['image_path'], '\\/'); - form_set_value(array('#parents' => array('image_path')), $widget['image_path']); + if (!preg_match('!^[^/].*[^/]$!', $widget['image_path'])) { + form_set_error('image_path', t('Image paths should not start or end with slashes.')); + } break; case 'save': - return array('max_resolution', 'max_filesize', 'max_number_images', 'image_path', 'file_extensions', 'custom_alt', 'custom_title'); + return array('max_resolution', 'max_filesize', 'image_path', 'file_extensions', 'custom_alt', 'custom_title'); } } @@ -441,7 +347,7 @@ function imagefield_widget_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_id, &$form) { +function imagefield_form_alter(&$form, $form_state, $form_id) { if ($form_id == '_content_admin_field') { $form['#attributes'] = array('enctype' => 'multipart/form-data'); } @@ -450,16 +356,16 @@ function imagefield_form_alter($form_id, /** * Create the image directory relative to the 'files' dir recursively for every * directory in the path. - * + * * @param $directory * The directory path under files to check, such as 'photo/path/here' * @param $form_element * A form element to throw an error on if the directory is not writable - */ + */ function imagefield_check_directory($directory, $form_element = array()) { - foreach(explode('/', $directory) as $dir) { + foreach (explode('/', $directory) as $dir) { $dirs[] = $dir; - $path = file_create_path(implode($dirs,'/')); + $path = file_create_path(implode($dirs, '/')); file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]); } return true; @@ -473,41 +379,17 @@ function _imagefield_scale_image($file, $result = image_scale($file['filepath'], $file['filepath'], $width, $height); if ($result) { $file['filesize'] = filesize($file['filepath']); - drupal_set_message(t('The image %filename was resized to fit within the maximum allowed resolution of %resolution pixels', array('%resolution' => $resolution, '%filename' => $file['filename']))); + drupal_set_message(t('The image %filename was resized to fit within the maximum allowed resolution of %resolution pixels', array('%resolution' => $resolution, '%filename' => $file['filename']))); } } } return $file; } - -function imagefield_clear_session() { - if (is_array($_SESSION['imagefield']) && count($_SESSION['imagefield'])) { - foreach (array_keys($_SESSION['imagefield']) as $fieldname) { - imagefield_clear_field_session($fieldname); - } - unset($_SESSION['imagefield']); - } -} - -function imagefield_clear_field_session($fieldname) { - if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) { - foreach ($_SESSION['imagefield'][$fieldname] as $delta => $file) { - if (is_file($file['filepath'])) { - file_delete($file['filepath']); - } - } - unset($_SESSION['imagefield'][$fieldname]); - } -} - function _imagefield_file_delete($file, $fieldname) { if (is_numeric($file['fid'])) { db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); } - else { - unset($_SESSION['imagefield'][$fieldname][$file['sessionid']]); - } module_invoke_all('imagefield_file', 'delete', $file); return file_delete($file['filepath']); } @@ -515,369 +397,266 @@ function _imagefield_file_delete($file, /** * Implementation of hook_widget(). */ -function imagefield_widget($op, &$node, $field, &$items) { - switch ($op) { - case 'default value': - return array(); +function imagefield_widget(&$form, &$form_state, $field, $items) { + $element = array( + '#type' => $field['widget']['type'], + '#title' => $field['widget']['label'], + '#default_value' => $items, + '#required' => $field['required'], + '#multiple' => $field['multiple'], + '#element_validate' => array('_imagefield_widget_validate'), + '#field' => $field, + ); + return $element; +} - case 'prepare form values': - _imagefield_widget_prepare_form_values($node, $field, $items); - return; +/** + * Implementation of hook_elements(). + */ +function imagefield_elements() { + $elements = array(); + $elements['image'] = array( + '#input' => TRUE, + '#columns' => array('fid', 'title', 'alt'), + '#process' => array('imagefield_element_process'), + ); + return $elements; +} - case 'form': - return _imagefield_widget_form($node, $field, $items); +/** + * Process the image type element before displaying the field. + */ +function imagefield_element_process($element, $edit, &$form_state, $form) { + $field = $element['#field']; + $fieldname = $field['field_name']; + drupal_add_css(drupal_get_path('module', 'imagefield') .'/imagefield.css'); - case 'validate': - _imagefield_widget_validate($node, $field, $items); - return; + // Pull in form state saved in storage. + form_get_cache($_POST['form_build_id'], $form_state); + if (isset($form_state['storage'][$fieldname])) { + $form_state['values'][$fieldname] = $form_state['storage'][$fieldname]; + // Unset the storage to keep the form from unnecessarily rebuilding. + unset($form_state['storage'][$fieldname]); } -} + // Change the type and theme to prevent FAPI from trying to output with the + // theme_image() function. + $element['#type'] = 'imagefield'; + $element['#theme'] = 'imagefield'; + $element['#tree'] = TRUE; -function _imagefield_widget_prepare_form_values(&$node, $field, &$items) { - $fieldname = $field['field_name']; - // clean up the session if we weren't posted. - if (!count($_POST)) { - imagefield_clear_session(); + $files = array(); + if (isset($form_state['values'])) { + // Set a default value if available. + if (!isset($form_state[$fieldname]) && isset($element['#default_value'])) { + $form_state['values'][$fieldname] = $element['#default_value']; + } + + foreach ($form_state['values'][$fieldname] as $delta => $file) { + if (!empty($file['fid'])) { + $files[$file['fid']] = array_merge($file, _imagefield_file_load($file['fid'])); + } + } } - // Attach new files - if ($file = file_check_upload($fieldname . '_upload')) { - $file = (array)$file; + $form_state['values'][$fieldname] = $files; + $element['#files'] = $files; - // Validation must happen immediately after the image is uploaded so we - // can discard the file if it turns out not to be a valid mime type - $valid_image = _imagefield_widget_upload_validate($node, $field, $items, $file); - //$valid_image = TRUE; + foreach ($files as $fid => &$file) { + $element[$fid]['flags']['delete'] = array( + '#type' => 'checkbox', + '#title' => t('Delete'), + '#default_value' => isset($file['flags']['delete']) ? $file['flags']['delete'] : 0, + ); - if ($valid_image) { - $file = _imagefield_scale_image($file, $field['widget']['max_resolution']); + $element[$fid]['admin_preview'] = array( + '#type' => 'markup', + '#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE), + ); - // Allow tokenized paths if available - if (function_exists('token_replace')) { - global $user; - $widget_image_path = token_replace($field['widget']['image_path'],'user', $user); - } - else { - $widget_image_path = $field['widget']['image_path']; - } - imagefield_check_directory($widget_image_path); - $filepath = file_create_filename($file['filename'], file_create_path($widget_image_path)); - - $file['fid'] = 'upload'; - $file['preview'] = $filepath; - - // If a single field, mark any other images for deletion and delete files in session - if (!$field['multiple']) { - if (is_array($items)) { - foreach ($items as $delta => $session_file) { - $items[$delta]['flags']['hidden'] = TRUE; - $items[$delta]['flags']['delete'] = TRUE; - } - } - imagefield_clear_field_session($fieldname); - } - // Add the file to the session - $file_id = count($items) + count($_SESSION['imagefield'][$fieldname]); - $file['sessionid'] = $file_id; - $_SESSION['imagefield'][$fieldname][$file_id] = $file; - } - else { - // Delete the invalid file - file_delete($file['filepath']); + $element[$fid]['description'] = array( + '#type' => 'markup', + '#value' => ''. t('Filename: ') .''. $file['filename'], + ); - // If a single field and a valid file is in the session, mark existing image for deletion - if (!$field['multiple']) { - if (count($_SESSION['imagefield'][$fieldname]) && count($items)) { - foreach ($items as $delta => $session_file) { - $items[$delta]['flags']['hidden'] = TRUE; - $items[$delta]['flags']['delete'] = TRUE; - } - } - } - } - } - - // Load files from preview state. before committing actions. - if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) { - foreach($_SESSION['imagefield'][$fieldname] as $delta => $file) { - $items[] = $file; + $element[$fid]['alt'] = array( + '#type' => 'hidden', + '#value' => $file['filename'], + ); + // overwrite with an input field if custom_alt is flagged; + if ($field['widget']['custom_alt']) { + $element[$fid]['alt'] = array( + '#type' => 'textfield', + '#title' => t('Alternate text'), + '#default_value' => $file['alt'], + '#description' => t('Alternate text to be displayed if the image cannot be displayed.'), + '#maxlength' => 255, + '#size' => 10, + ); } - } -} -function _imagefield_widget_form($node, $field, &$items) { - drupal_add_js('misc/progress.js'); - drupal_add_js('misc/upload.js'); + $element[$fid]['title'] = array( + '#type' => 'hidden', + '#value' => $file['filename'], + ); + // overwrite with an input field if custom_title is flagged; + if ($field['widget']['custom_title']) { + $element[$fid]['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => $file['title'], + '#description' => t('Text to be displayed on mouse overs.'), + '#maxlength' => 255, + '#size' => 10, + ); + } - $fieldname = $field['field_name']; - drupal_add_css(drupal_get_path('module', 'imagefield') .'/imagefield.css'); - - $form = array(); + elseif ($file['filepath'] && $file['flags']['hidden']) { + // Render all the form values of this item if it is hidden. + $element[$fid]['flags']['hidden'] = array('#type' => 'value', '#value' => $file['flags']['hidden']); + $element[$fid]['flags']['delete'] = array('#type' => 'value', '#value' => $file['flags']['delete']); + $element[$fid]['title'] = array('#type' => 'value', '#value' => $file['title']); + $element[$fid]['alt'] = array('#type' => 'value', '#value' => $file['alt']); + } + + if ($element['#multiple']) { + $element[$fid]['weight'] = array( + '#type' => 'weight', + '#delta' => max('10', count($element['#files'])), + ); + } - $form[$fieldname] = array( - '#type' => 'fieldset', - '#title' => t($field['widget']['label']), - '#weight' => $field['widget']['weight'], - '#collapsible' => TRUE, - '#collapsed' => FALSE, - '#tree' => TRUE, - '#prefix' => '