fileupload_form

5 fileupload.module fileupload_form(&$node)

Implementation of hook_form().

Return an array of the form elements needed to edit this node.

File

developer/examples/fileupload.module, line 121
This is an example to demonstrate how to make a Drupal node support file uploads.

Code

function fileupload_form(&$node) {
  // Set form parameters so we can accept file uploads.
  $form['#attributes'] = array('enctype' => 'multipart/form-data');
  // standard node title
  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => t('Title'), 
    '#size' => 60, 
    '#maxlength' => 128, 
    '#required' => TRUE, 
    '#default_value' => $node->title,
  );
  // file upload field
  $form['file'] = array(
    '#type' => 'file', 
    '#title' => t('File'), 
    '#size' => 40, 
    '#default_value' => '',
  );
  if ($node->file->filepath) {
    $form['file']['#description'] .= t('A file already exists, if you upload another file the current file will be replaced.');
  }

  if ($node->nid) {
    // the validate function doesn't get a full copy of the node, only the
    // data posted from the form. use this field to tell the validator
    // the path of the node's current file.
    $form['current_file'] = array(
      '#type' => 'value', 
      '#value' => $node->file->filepath,
    );
  }

  return $form;
}
Login or register to post comments