With Drupal 6 cck module we can easily customize existing field or adding a extra field such as a new file field able to uploading file via AJAX. Each content note can support multiply files uploading and connecting to that node itself, however, what we wanna do is adding a new custom field for each uploaded file other than the whole content node.

The default only include a text field called "description" in the file uploading widget. In order to complete that task, we do:
1). Hack the File Field module, adding a extra text field (called paragraph) and able to write the data into database.
Here, in filefield.theme.inc, find function function filefield_widget_process($element, $edit, &$form_state, $form), insert the block of code as:

    $element['data']['paragraph'] = array(
      '#type' => 'textfield',
      '#title' => t('Paragraph'),
      '#value' => isset($item['data']['paragraph']) ? $item['data']['paragraph'] : '',
      '#type' => variable_get('filefield_paragraph_type', 'textfield'),
      '#maxlength' => variable_get('filefield_paragraph_length', 128),
    );

in filefield.token.inc
add a new element as $tokens['file']['filefield-paragraph'] simliar to $tokens['file']['filefield-description'].

We can find the new "paragraph" field data would be reseved in "content_field_upload" table.

2). Get the data from database, in filefield_formatter.inc file, find function function theme_filefield_file($file), modify the return string as:


/**
 * Theme function for the 'generic' single file formatter.
 */
function theme_filefield_file($file) {
  // Views may call this function with a NULL value, return an empty string.
  if (empty($file['fid'])) {
    return '';
  }

  $path = $file['filepath'];
  $url = file_create_url($path);
  $icon = theme('filefield_icon', $file);

  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  // TODO: Possibly move to until I move to the more complex format described 
  // at http://darrelopry.com/story/microformats-and-media-rfc-if-you-js-or-css
  $options = array(
    'attributes' => array(
      'type' => $file['filemime'] . '; length=' . $file['filesize'],
    ),
  );

  // Use the description as the link text if available.
  if (empty($file['data']['description'])) {
    $link_text = $file['filename'];
  }
  else {
    $link_text = $file['data']['description'];
    $options['attributes']['title'] = $file['filename'];
  }

  return '<div class="filefield-file">'. $icon . l($link_text, $url, $options) . $file['data']['paragraph'] . '</div>';
}

Comments

hxyconan’s picture

Category: task » support
quicksketch’s picture

Status: Active » Fixed

You can just use http://drupal.org/project/imagefield_extended, which utilizes this same mechanism. It works on FileField as well as ImageField.

hxyconan’s picture

Status: Fixed » Active

Other than adding the extra field into file field upload widget, we ganna do the same thing with mp3player module.

In filefieldmp3player.module that includes function to grap data from database then display on the screen.
Change the code to:

  else if(isset($item['data']['description']) && is_array($item['data'])) {
    //Data straight from node isn't serialized
    $description = '<h6 class=mp3player_description_2_title>'. $item['data']['description'] . '</h6>' . '<p class=mp3player_description_2_para>' . $item['data']['description_2'] . '</p>';
  }
hxyconan’s picture

Correct:
filefield_widget_process($element, $edit, &$form_state, $form)
is in filefield_widget.inc.

hxyconan’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

NitsPatel’s picture

How to add extra field in file upload widget in drupal7