0

This question already has an answer here:

I'm trying to make custom form and add file field to it. Besides, I want to add uploaded files to this form. Here is the code.

function node_stage_stage_report_form($form,&$form_state,$nid)
{ 
   $stage_node = node_load($nid);

   $langcode = 'und';
   $bundle = "node_stage";
   $entity_type = 'node';
   $field = field_info_field('stage_report');
   $instance = field_info_instance($entity_type,'stage_report',$bundle);
   $items = $stage_node->stage_report;
   $form['#parents'] = array();

   $element = array(
     '#entity' => $stage_node,
     '#entity_type' => $instance['entity_type'],
     '#bundle' => $instance['bundle'],
     '#field_name' => 'stage_report',
     '#language' => $langcode,

     '#field_parents' => array(),
     '#file' => array(),
     '#columns' => array_keys($field['columns']),
     '#title' => check_plain($instance['label']),
     '#description' => $instance['description'],
     '#required' => FALSE,
  );
  $result = field_default_form('node',$stage_node,$field,$instance,$langcode,$items,$form,$form_state);

  $result2 = field_view_field('node',$stage_node,$field['field_name']);

  if(is_array($result))
     $form =array_merge($form,$result);
  else 
    $form[] = $result;   
  $form['submit']= array(
    '#type' => 'submit',
    '#value' => t("Add files"),
  );

 return $form;

}

The current way using field_view_field is not correct as I want to make the field look like in edit form. So, what's the way to do it in custom form?

marked as duplicate by Clive Jul 18 '13 at 15:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you asking how to get a filefield widget into a custom form? – Clive Jul 18 '13 at 15:42
  • yes, filefield widget with multiple uploaded files and file uploading form for that field – alivenets Jul 18 '13 at 15:43
  • Thought so, check the duplicate :) – Clive Jul 18 '13 at 15:44

Browse other questions tagged or ask your own question.