Display a CCK Filefield or Imagefield Upload Widget on Your Own Custom Form

Took a fair amount of googling around to find the solution to this one. With the Node Gallery 3.x branch, we needed a way to quickly add an image to an existing gallery. We could have displayed the whole node form, but there's a lot of things on that form that we can just use the defaults for 99% of the time. We need just three fields filled in: Title, Caption, and the imagefield itself.

To use the same imagefield widget that handles all the hard work for you on the node add field on your own field, first create a handler in hook_menu such as this:

  1.   $items['node/%node_gallery_gallery/upload'] = array(
  2.     'title' => 'Upload New Image',
  3.     'page callback' => 'drupal_get_form',
  4.     'page arguments' => array('node_gallery_upload_image_form', 1),
  5.     'access callback' => 'node_gallery_user_access',
  6.     'access arguments' => array('upload', 1),
  7.     'file' => 'node_gallery.pages.inc',
  8.     'type' => MENU_LOCAL_TASK,
  9.   );

Then, in node_gallery.pages.inc, you create the form function that does the work:

  1. function node_gallery_upload_image_form($form_state, $gallery) {
  2.   $imagetype = 'node_gallery_image';
  3.   $form_id = $imagetype . '_node_form';
  4.  
  5.   module_load_include('inc', 'content', 'includes/content.node_form');
  6.  
  7.   $field = content_fields('field_node_gallery_image',$imagetype);
  8.  
  9.   $form['title'] = array(
  10.     '#title' => t('Title'),
  11.     '#type' => 'textfield',
  12.     '#required' => TRUE,
  13.     '#weight' => -10,
  14.   );
  15.   $form['body'] = array(
  16.     '#title' => t('Caption'),
  17.     '#type' => 'textarea',
  18.     '#weight' => -9,
  19.   );
  20.   $form['type'] = array(
  21.     '#type' => 'value',
  22.     '#value' => $imagetype,
  23.   );
  24.   $form['gid'] = array(
  25.     '#type' => 'value',
  26.     '#value' => $gallery->nid,
  27.   );
  28.   $form['#field_info']['field_node_gallery_image'] = $field;
  29.   $form['#field_info']['field_node_gallery_image']['#required'] = TRUE;
  30.   $form += (array) content_field_form($form, $form_state, $field);
  31.  
  32.   $form['submit'] = array('#type' => 'submit', '#weight' => 10, '#value' => 'Save');
  33.  
  34.   return $form;

This is pretty straightforward, up until lines 28 - 30. Those three lines setup the form array and then append the results from content_field_form() to our existing form. Still, very easy, but I wasn't able to find any documentation on how to do this. Just in case you're curious, here's the submit handler for that form.

  1. function node_gallery_upload_image_form_submit($form, &$form_state) {
  2.   global $user;
  3.   $image = new stdClass;
  4.   $image->uid = $user->uid;
  5.   $image->name = (isset($user->name) ? $user->name : '');
  6.   $values = $form_state['values'];
  7.   foreach ($values as $key => $value) {
  8.     $image->$key = $value;
  9.   }
  10.   node_gallery_image_save($image);
  11. }

Nothing new there. The end result is a nice looking, concise form that allows you to quickly upload an image to a gallery. Sweet!

Your rating: None Average: 4 (2 votes)

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.

More information about formatting options