Community

How to build a table form like the User admin form at admin/user/user with checkboxes inside

I'm not sure if this post belongs here or in the theme forum. Please excuse if I have it wrong.

I am trying to build a form that looks and works much like the User admin table form at admin/user/user. But one that displays different data, and does different operations.

I tried lifting the code straight out of the User module, just to see if I could recreate the form on my own page from my own module. (I figure to come back and change the SQL and such after I get the original form working.)

file: user.admin.inc
/**
* Form builder; User administration page.
*
* @ingroup forms
* @see user_admin_account_validate()
* @see user_admin_account_submit()
*/
function user_admin_account() {
etc...

Of course I changed the function name.

I get the data OK, and a form is built. I even get the pager OK. But I am not getting the table layout.

I'm pretty much tearing my hair out trying to figure out how the table is built with the form checkboxes inside.

Any advice would be deeply appreciated.

Thanks for reading.

Comments

It's tricky

Getting tables to work right is pretty tricky. I've managed to do it, but it isn't pretty.

Try looking here: http://drupalcode.org/project/datereminder.git/blob/9d226060c25b41d2dc8e... for an example.

There are actually two different variants of tables in there, used for different purposes. I really should combine them, but haven't gotten around to it.

The trick is, in your theme function, you need to build two arrays, one that's a list of headers, one that's a list of rows with each row a list of rendered cells.

You end up with a loop something like this:

$headers = array(<header elements>);
$rows = [];
foreach ($rowlist as $r) {
    $row = [];
    foreach ($columns as $c) {
        $row[] = drupal_render($variables['form'][however you find your individual elements]);
    }
    $rows[] = $row;
}
$output = theme('table', array('header' => $header, 'rows' => $rows));
$output .= drupal_render_children($variables['form']);
return $output;

I can't guarantee that's exactly right,

Note that it's important to start all of the way up at $variable['form'] in that drupal_render() call. Otherwise Drupal doesn't mark your cells as already rendered, and you'll end up getting them twice. (Or at least that's what I found. Someone please point out an alternative if I'm wrong.)

Here's a tutorial I wrote for

Here's a tutorial I wrote for D6 a couple of years back: http://www.jaypan.com/tutorial/themeing-drupal-6-forms-tables-checkboxes

Jaypan We build websites

That looks helpful!

Jaypan - Dang, I wish I'd had that when I was trying to figure that all out.