[development] Forms API Question - Node with table like poll.module

andrew morton drewish at katherinehouse.com
Sat Nov 12 01:15:02 UTC 2005


I'm doing a little of the same kind of updates to some modules and
I've found that the system.module settings forms are an excellent
example of how to do form tables.

The basic idea is to build the form, then put all the table rendering
into a theme function. As you build the table you call form_render()
on the contents of each cell. Finally, you call:
$output .= theme('table',...)
$output .= form_render($form);
return $output;
The cool part is that anything you haven't rendered in the table (like
submission buttons) gets rendered below it.

If that doesn't make sense or something's incorrect let me know, as I
said I've just kind of figured it out.

andrew

On 11/11/05, Simon Lindsay <simon at dirtbike.ws> wrote:
>
> Hello All,
>
> I'm developing some modules to manage my computer business, and to do
> invoice, purchase orders, timesheets etc. I was using a table in the
> form in 4.6.3, but things are different in 4.7.
>
> The code below looks the closert so far, is there a better way to do
> this anybody can recommend? I've looked at the modules that use
> arrays/rows, like poll and system, but they aren't really doing what i'm
> trying to.
>
> Or do I just need to use css in the theme it to get the correct display?
>
> I'm open to suggestions.
>
> TIA
>
> Simon
>
> 4.6.3
> -------
>    $header = array(t('Time'), t('Customer'), t('Comments'));
>    $rows = array();
>    $count = count($node->timesheet_time);
>    if ($count < 5) {
>      $count = 5;
>    }
>    if ($_POST['edit']['timesheet_links_more'] == 1) {
>      $count += 5;
>    }
>    for ($i = 0; $i < $count; $i++) {
>      $row = array();
>      $row[] = form_textfield('', 'timesheet_time][' . $i,
> $node->timesheet_time[$i], 6, 6);
>      $row[] = form_autocomplete('', 'timesheet_customer][' . $i,
> $node->timesheet_customer[$i], 30, 128,
> 'contacts/autocomplete_customer', $type);
>      $row[] = form_textfield('', 'timesheet_comments][' . $i,
> $node->timesheet_comments[$i], 30, 255);
>      //$row[] = form_textarea('', 'timesheet_comments][' . $i,
> $node->timesheet_comments[$i], 20, 2);
>
>      $rows[$i] = $row;
>    }
>
>    $output .= form_item(t('Timesheet Entries'), theme('table', $header,
> $rows));
>
> 4.7
> ------
>
>    // If the user pressed the button asking for more rows, add more
>    if ($_POST['edit']['timesheet_links_more'] == 1) {
>      $count += 5;
>    }
>
>    $form['entries'] =
>      array(
>            '#type' => 'fieldset',
>            '#title' => t('Timesheet Entries'),
>            '#tree' => TRUE,
>            );
>
>    // Now actually display the rows
>    for ($i = 0; $i < $count; $i++) {
>      $form['entries'][$i] =
>        array(
>              '#type' => 'fieldset',
>              '#tree' => TRUE,
>              );
>      $form['entries'][$i]['timesheet_time'] =
>        array(
>              '#type' => 'textfield',
>              '#size' => 6,
>              '#maxlength' => 6,
>              '#default_value' => $node->timesheet_time[$i],
>              '#prefix' => '<table><tr><td>',
>              '#suffix' => '</td>',
>              );
>
>      $form['entries'][$i]['timesheet_customer'] =
>        array(
>              '#type' => 'textfield',
>              '#size' => 30,
>              '#maxlength' => 128,
>              '#autocomplete_path' => 'customer/autocomplete',
>              '#default_value' => $node->timesheet_customer[$i],
>              '#prefix' => '<td>',
>              '#suffix' => '</td>',
>              );
>
>      $form['entries'][$i]['timesheet_comments'] =
>        array(
>              '#type' => 'textfield',
>              '#size' => 30,
>              '#maxlength' => 255,
>              '#default_value' => $node->timesheet_comments[$i],
>              '#prefix' => '<td>',
>              '#suffix' => '</td></tr></table>',
>              );
>
>    }
>
>


More information about the development mailing list