You are here

function _filefield_widget_form in FileField 5

Same name and namespace in other branches
  1. 5.2 filefield.module \_filefield_widget_form()
1 call to _filefield_widget_form()
filefield_widget in ./filefield.module
Implementation of hook_widget().

File

./filefield.module, line 337
Defines a file field type.

Code

function _filefield_widget_form($node, $field, &$node_field) {
  $fieldname = $field['field_name'];
  drupal_add_css(drupal_get_path('module', 'filefield') . '/filefield.css');
  $form = array();
  $form[$fieldname] = array(
    '#type' => 'fieldset',
    '#title' => t($field['widget']['label']),
    '#weight' => $field['widget']['weight'],
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#theme' => 'filefield_current',
  );
  $form[$fieldname][$fieldname . '_upload'] = array(
    '#type' => 'file',
    '#description' => $field['widget']['description'] . t('<br />allowed extensions(%ext)', array(
      '%ext' => $field['widget']['file_extensions'],
    )),
    '#weight' => 9,
    '#tree' => FALSE,
  );
  $form[$fieldname]['update'] = array(
    '#type' => 'button',
    '#value' => t('Upload'),
    '#name' => 'cck_filefield_' . $fieldname . '_op',
    '#attributes' => array(
      'id' => $fieldname . '-attach-button',
    ),
    '#tree' => FALSE,
    '#weight' => 10,
  );
  if (is_array($node_field) && count($node_field)) {
    foreach ($node_field as $delta => $file) {
      if ($file['filepath'] && !$file['remove']) {
        $form[$fieldname][$delta]['icon'] = array(
          '#type' => 'markup',
          '#value' => theme('filefield_icon', $file),
        );
        $filepath = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path()) : $file['filepath'];
        $description = file_create_url($filepath);
        $description = "<small>" . check_plain($description) . "</small>";
        $form[$fieldname][$delta]['description'] = array(
          '#type' => 'textfield',
          '#default_value' => strlen($file['description']) ? $file['description'] : $file['filename'],
          '#maxlength' => 256,
          '#description' => $description,
        );
        $form[$fieldname][$delta]['size'] = array(
          '#type' => 'markup',
          '#value' => format_size($file['filesize']),
        );
        $form[$fieldname][$delta]['remove'] = array(
          '#type' => 'checkbox',
          '#default_value' => $file['remove'],
        );
        $form[$fieldname][$delta]['list'] = array(
          '#type' => 'checkbox',
          '#default_value' => $file['list'],
        );
        $form[$fieldname][$delta]['filename'] = array(
          '#type' => 'value',
          '#value' => $file['filename'],
        );
        $form[$fieldname][$delta]['filepath'] = array(
          '#type' => 'value',
          '#value' => $file['filepath'],
        );
        $form[$fieldname][$delta]['filemime'] = array(
          '#type' => 'value',
          '#value' => $file['filemime'],
        );
        $form[$fieldname][$delta]['filesize'] = array(
          '#type' => 'value',
          '#value' => $file['filesize'],
        );
        $form[$fieldname][$delta]['fid'] = array(
          '#type' => 'value',
          '#value' => $file['fid'],
        );

        // Special handling for single value fields
        // mark original item for deletion.
        if (!$field['multiple']) {
          $form[$fieldname][$delta]['replace'] = array(
            '#type' => 'markup',
            '#value' => t('If a new file is uploaded, this file will be replaced upon submitting this form.'),
          );
        }
      }
      elseif ($file['filepath'] && $file['remove']) {
        $form[$fieldname][$delta]['remove'] = array(
          '#type' => 'hidden',
          '#value' => $file['remove'],
        );
      }
    }
  }
  return $form;
}