You are here

function _filefield_widget_form in FileField 5.2

Same name and namespace in other branches
  1. 5 filefield.module \_filefield_widget_form()
2 calls to _filefield_widget_form()
filefield_js in ./filefield.module
Menu callback for JavaScript-based uploads.
filefield_widget in ./filefield.module
Implementation of hook_widget().

File

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

Code

function _filefield_widget_form($node, $field, &$items) {
  drupal_add_js('misc/progress.js');
  drupal_add_js('misc/upload.js');
  drupal_add_js(drupal_get_path('module', 'filefield') . '/filefield.js');
  $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']),
    '#description' => t('Changes made to the attachments are not permanent until you save this post.'),
    '#weight' => $field['widget']['weight'],
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#prefix' => '<div id="' . form_clean_id($fieldname . '-attach-wrapper') . '">',
    '#suffix' => '</div>',
  );
  $form[$fieldname]['new'] = array(
    '#tree' => FALSE,
    '#prefix' => '<div id="' . form_clean_id($fieldname . '-attach-hide') . '">',
    '#suffix' => '</div>',
    '#weight' => 100,
  );

  // Construct the upload description out of user supplied text,
  // maximum upload file size, and (optionally) allowed extensions.
  $upload_description = empty($field['widget']['description']) ? '' : $field['widget']['description'] . '<br/>';
  $upload_description .= t('Maximum file size: !size.', array(
    '!size' => format_size(file_upload_max_size()),
  ));
  if (!empty($field['widget']['file_extensions'])) {
    $upload_description .= ' ' . t('Allowed extensions: %ext.', array(
      '%ext' => $field['widget']['file_extensions'],
    ));
  }

  // Separate from tree becase of that silly things won't be displayed
  // if they are a child of '#type' = form issue
  $form[$fieldname]['new'][$fieldname . '_upload'] = array(
    '#type' => 'file',
    '#title' => t('Attach new file'),
    '#description' => $upload_description,
    '#weight' => 9,
    '#tree' => FALSE,
    '#attributes' => array(
      'accept' => str_replace(' ', ',', trim($field['widget']['file_extensions'])),
    ),
  );
  $form[$fieldname]['new']['upload'] = array(
    '#type' => 'button',
    '#value' => t('Upload'),
    '#name' => 'cck_filefield_' . $fieldname . '_op',
    '#id' => form_clean_id($fieldname . '-attach-button'),
    '#tree' => FALSE,
    '#weight' => 10,
  );
  if (is_array($items) && count($items)) {
    $form[$fieldname]['files'] = array(
      '#parents' => array(
        $fieldname,
      ),
      '#theme' => 'filefield_form_current',
      // remember the force_list setting so that the theme function knows
      '#force_list' => $field['force_list'],
    );
    foreach ($items as $delta => $file) {

      // @todo: split into its own form and theme functions per file like imagefield
      if ($file['filepath'] && !$file['delete']) {
        $form[$fieldname]['files'][$delta] = _filefield_file_form($node, $field, $file);
      }
      else {
        if ($file['filepath'] && $file['delete']) {
          $form[$fieldname]['files'][$delta]['delete'] = array(
            '#type' => 'hidden',
            '#value' => $file['delete'],
          );
        }
      }
    }

    // Special handling for single value fields.
    if (!$field['multiple']) {
      $form[$fieldname]['replace'] = array(
        '#type' => 'markup',
        '#value' => '<p>' . t('If a new file is uploaded, this file will be replaced upon submitting this form.') . '</p>',
        '#prefix' => '<div class="description">',
        '#suffix' => '</div>',
      );
    }
  }

  // The class triggers the js upload behaviour.
  $form[$fieldname . '-attach-url'] = array(
    '#type' => 'hidden',
    '#value' => url('filefield/js', NULL, NULL, TRUE),
    '#attributes' => array(
      'class' => 'upload',
    ),
  );

  // Some useful info for our js callback.
  $form['vid'] = array(
    '#type' => 'hidden',
    '#value' => $node->vid,
    '#tree' => FALSE,
  );
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid,
    '#tree' => FALSE,
  );
  $form['type'] = array(
    '#type' => 'hidden',
    '#value' => $node->type,
    '#tree' => FALSE,
  );
  return $form;
}