Example 6: Theme a Form with TAPIr

This example models using TAPIr to theme a form. Form data may be passed from a form's theme function to tapir_get_table so the form elements may be rendered in the table. The example here themes a form that contains a textfield, but you can of course theme any sized form. It requires of course the form builder function, a theme function for that form, and a table builder function. For the data to be saved, you'd use the Forms API submit hook like normal.

If a TAPIr table is being used to theme a form, you are advised to lock the fields containing input elements so the site owner can't break stuff. (Unless of course it's an optional element.) The settings page is included to give an example of a locked field. Notice, too, that the settings array for the table disables the table preview, as it would show an error since no form is built in the preview.

Example: view page | settings (no backlink on settings page; opens in a new window)

Code:

<?php
function tapirtest_menu($may_cache) {
  if (
$may_cache) {
   
$items[] = array(
     
'path' => 'tapirtest/example_6',
     
'title' => t('Example 6: Theme a Form with TAPIr'),
     
'callback' => 'tapirtest_example_6',
     
'access' => user_access('access content'),
     
'type' => MENU_CALLBACK,
    );
  }
  return
$items;
}

function

tapirtest_table_settings() {
 
$tables[] = array(
   
'id' => 'tapirtest_example_table_6',
   
'description' => t('A settings page with form input columns locked.'),
   
'path' => 'tapirtest/example_6',
   
'access' => 'access content',
   
'preview' => FALSE,
  );
  return
$tables;
}

function

tapirtest_example_6() {
 
$output = '<p>'. drupal_get_form('tapirtest_example_6_form') .'</p>';
  return
$output;
}

function

tapirtest_example_6_form() {
 
$form['songs'] = array('#tree' => TRUE);
 
$result = db_query("SELECT * FROM {tapirtest_example_5}");
  while (
$row = db_fetch_object($result)) {
   
$form['songs'][$row->song_id]['song'] = array(
     
'#value' => $row->song,
    );
   
$form['songs'][$row->song_id]['artist'] = array(
     
'#type' => 'textfield',
     
'#size' => 16,
     
'#default_value' => $row->artist,
    );
   
$form['songs'][$row->song_id]['album'] = array(
     
'#type' => 'textfield',
     
'#size' => 32,
     
'#default_value' => $row->album,
    );
  }
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Submit'),
  );
  return
$form;
}

function

theme_tapirtest_example_6_form($form) {
 
$output = '<p>'. tapir_get_table('tapirtest_example_table_6', $form)
          .
drupal_render($form) .'</p>';
  return
$output;
}

function

tapirtest_example_table_6($op, &$form) {
  switch (
$op) {
    case
'fields':
     
$fields[] = array('name' => 'song', 'title' => t('Song'), 'weight' => 0, 'enabled' => TRUE);
     
$fields[] = array('name' => 'artist', 'title' => t('Artist'), 'weight' => 1,
                      
'enabled' => TRUE, 'locked' => TRUE);
     
$fields[] = array('name' => 'album', 'title' => t('Album'), 'weight' => 2,
                      
'enabled' => TRUE, 'locked' => TRUE);
      return
$fields;
    case
'data':
      foreach (
element_children($form['songs']) as $i) {
       
$data['song'][] = drupal_render($form['songs'][$i]['song']);
       
$data['artist'][] = drupal_render($form['songs'][$i]['artist']);
       
$data['album'][] = drupal_render($form['songs'][$i]['album']);
      }
      return
$data;
  }
}
?>