If anyone has a better way to do this, please let me know. If anyone find it useful, then it's great.
Based on my understanding, the standard way to theme a form using TAPIR is as following
(see http://www.ubercart.org/docs/tapir/3000/example_6_theme_form_tapir)
<?php
// main code to call drupal form
drupal_get_form('tapirtest_example_6_form');
...
function theme_tapirtest_example_6_form($form) {
$output = '<p>'. tapir_get_table('tapirtest_example_table_6', $form)
. drupal_render($form['submit']) .'</p>'; // add submit button, not the whole form
return $output;
}
// form builder function
function tapirtest_example_6_form() // I want to reuse this function with different parameters
{
$form['songs'] = array('#tree' => TRUE);
$result = db_query("SELECT * FROM {tapirtest_example_5}"); // I want to add parameters in sql string
while ($row = db_fetch_object($result)) {
$form['songs'][$row->song_id]['song'] = array(
...
}
// table builder function
function tapirtest_example_table_6($op, $form) {
switch ($op) {
case 'fields':
$fields[] = array('name' => 'song', 'title' => t('Song'), 'weight' => 0, 'enabled' => TRUE);
...
}
?>What I want to do is to reuse the form builder function with parameters. I'd prefer to pass the parameters from drupal_get_form() to the form builder function, I couldn't find a way to do it, so I added extra hard coded function names, then these functions call the form builder with the parameters.
It looked really stupid, but I can reuse the form builder, and table builder.
<?php
// get artist john
drupal_get_form('artist_john_form');
// get artist jane
drupal_get_form('artist_jane_form');
function theme_artist_john_form($form) {
$output = '<p>'. tapir_get_table('tapirtest_example_table_6', $form)
. drupal_render($form['submit']) .'</p>';
return $output;
}
function artist_john_form()
{
return tapirtest_example_6_form('john');
}
function theme_artist_jane_form($form) {
$output = '<p>'. tapir_get_table('tapirtest_example_table_6', $form)
. drupal_render($form['submit']) .'</p>';
return $output;
}
function artist_jane_form()
{
return tapirtest_example_6_form('jane');
}
// form builder function
function tapirtest_example_6_form($artist='') // added one parameter
{
$where = "";
if ($artist != '')
$where = " where artist ='".$artist."'";
$form['songs'] = array('#tree' => TRUE);
$result = db_query("SELECT * FROM {tapirtest_example_5} ".$where); // find the artist
while ($row = db_fetch_object($result)) {
$form['songs'][$row->song_id]['song'] = array(
...
}
// table builder unchanged
?>The limit is that you can only add a few functions with parameters you already know in advance.




Joined: 02/28/2008