/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Themeing function to render a form as a table. * * The $form array should contain the following: * - $form['heading'], a form item of the default (markup) type (thus with * only #value set), containing an (optional) heading. * - $form['header'], an array of form items of the default (markup) type * (thus with only #value set), containing the header fields * - $form['rows'], an array of form items containing the rows. Each row is * again an array of form items. * - $form['empty'], if this is set, then no table will be rendered. You may * want to put an "empty message" in here. * * When $form['empty'] is not set, a table will be rendered first. After that, * all other form items will get rendered. * If $form['empty'] is set, $form['header'] and $form['rows] will be deleted, * and then all other form items will get rendered. */ function theme_mymodule_table_form($form) { $output = ''; // We always want the heading to be rendered first. $output .= drupal_render($form['heading']); if (!$form['empty']) { // Render $form['header'] as the header of the table. foreach ($form['header'] as $field) { $header[]['data'] = $field['#value']; } // Render $form['rows'] as the rows of the table. foreach (element_children($form['rows']) as $i) { foreach (element_children($form['rows'][$i]) as $key) { $row[]['data'] = drupal_render($form['rows'][$i][$key]); } $rows[] = $row; } // Render the table. } else { // Delete the header and rows, just to be sure. // Render the empty message. $output .= drupal_render($form['empty']); } // Render the remaining form items. $output .= drupal_render($form); return $output; }