array( 'label' => t('Image'), 'field types' => array('file'), ), 'image_link_content' => array( 'label' => t('Image linked to content'), 'field types' => array('file'), ), 'image_link_file' => array( 'label' => t('Image linked to file'), 'field types' => array('file'), ), ); // TODO: Add image style formatters. return $formatters; } /** * Implement hook_field_widget_info(). */ function image_field_widget_info() { return array( 'file_image' => array( 'label' => t('Image'), 'field types' => array('file'), 'settings' => array( 'file_extensions' => 'png gif jpg jpeg', 'file_directory' => '', 'progress_indicator' => 'throbber', 'max_filesize_per_field' => '', 'max_filesize_per_file' => '', 'alt_field' => 0, 'title_field' => 0, 'caption_field' => 0, ), 'behaviors' => array( 'multiple values' => FIELD_BEHAVIOR_DEFAULT, 'default value' => FIELD_BEHAVIOR_NONE, ), ), ); } /** * Implementation of hook_field_widget(). */ function image_field_widget(&$form, &$form_state, $field, $instance, $items, $delta = 0) { $form['#attributes'] = array('enctype' => 'multipart/form-data'); $item = array( 'fid' => 0, 'list' => $field['settings']['list_default'], 'data' => array('alt' => '', 'title' => '', 'caption' => ''), ); if (isset($items[$delta])) { $item = array_merge($item, $items[$delta]); } // Essentially we use the managed_file type, extended with some enhancements. $element_info = element_info('managed_file'); $element = array( '#title' => $instance['label'], '#type' => 'managed_file', '#default_value' => $item, '#required' => $instance['required'], '#save_directory' => file_field_widget_file_path($instance), '#upload_validators' => file_field_widget_upload_validators($field, $instance), '#value_callback' => 'file_field_widget_value', '#process' => array_merge($element_info['#process'], array('image_field_widget_process')), // Allows this field to return an array instead of a single value. '#extended' => TRUE, ); return $element; } /** * An element #process callback for the file_image field type. * * Expands the file_image type to include the alt, title, and caption fields. */ function image_field_widget_process($element, &$form_state, $form) { $item = $element['#value']; $item['fid'] = $element['fid']['#value']; $field = field_info_field($element['#field_name']); $instance = field_info_instance($element['#field_name'], $element['#bundle']); $defaults = field_info_widget_settings($instance['widget']['type']); $settings = array_merge($defaults, $instance['widget']['settings']); $element['#theme'] = 'image_widget'; // Add the list field if enabled. if ($field['settings']['list_field'] && $item['fid']) { $element['list'] = array( '#type' => empty($item['fid']) ? 'hidden' : 'checkbox', '#title' => t('Include file in display'), '#value' => isset($item['list']) ? $item['list'] : $field['list_default'], '#attributes' => array('class' => 'file-list'), ); } else { $element['list'] = array( '#type' => 'hidden', '#value' => '1', ); } // Data placeholder for widgets to store additional data. $element['data'] = array( '#tree' => 'true', '#title' => t('File data'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#access' => (bool) $item['fid'], ); // Check if using the default alt text and replace tokens. $default_alt = (!$settings['alt_field'] || (empty($file['status']) && empty($file['data']['alt']))); $element['data']['alt'] = array( '#title' => t('Alternate Text'), '#type' => $settings['alt_field'] ? 'textfield' : 'value', '#default_value' => $default_alt ? $settings['alt_default'] : $file['data']['alt_default'], '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), '#maxlength' => variable_get('image_alt_length', 80), // See http://www.gawds.org/show.php?contentid=28. ); if ($element['data']['alt']['#type'] == 'value') { $element['data']['alt']['#value'] = $settings['alt']; } // Check if using the default title and replace tokens. $default_title = (!$settings['title_field'] || (empty($item['status']) && empty($item['data']['title']))); // If the custom title is enabled, input type defaults to textfield. if (!empty($settings['title_custom'])) { $title_type = !empty($settings['title_type']) ? $settings['title_type'] : 'textfield'; } else { $title_type = 'value'; } $element['data']['title'] = array( '#type' => $title_type, '#title' => t('Title'), '#default_value' => $default_title ? $settings['title_default'] : $file['data']['title_default'], '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'), '#maxlength' => variable_get('image_title_length', 500), ); if ($element['data']['title']['#type'] == 'value') { $element['data']['title']['#value'] = $settings['title_default']; } return $element; } function theme_image_widget($element) { $output = ''; $output .= '
'; return $output; } /** * Theme function for 'image' file field formatter. */ function theme_field_formatter_image($element) { return theme('image', $element['#item']['filepath'], $element['#item']['data']['alt'], $element['#item']['data']['title']); } /** * Theme function for 'image_link_content' file field formatter. */ function theme_field_formatter_image_link_content($element) { list($id, $vid, $bundle) = field_attach_extract_ids($element['#object_type'], $element['#object']); l(theme('field_formatter_image', $element), $element['#object_type'] . '/' . $id); } /** * Theme function for 'image_link_file' file field formatter. */ function theme_field_formatter_image_link_file($element) { l(theme('field_formatter_image', $element), file_create_url($element['#item']['filepath'])); }