Posted By

WimLeers on 10/28/07


Tagged

textmate drupal FAPI


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

WimLeers


table form themeing function


 / Published in: PHP
 

  1. /**
  2.  * Themeing function to render a form as a table.
  3.  *
  4.  * The $form array should contain the following:
  5.  * - $form['heading'], a form item of the default (markup) type (thus with
  6.  * only #value set), containing an (optional) heading.
  7.  * - $form['header'], an array of form items of the default (markup) type
  8.  * (thus with only #value set), containing the header fields
  9.  * - $form['rows'], an array of form items containing the rows. Each row is
  10.  * again an array of form items.
  11.  * - $form['empty'], if this is set, then no table will be rendered. You may
  12.  * want to put an "empty message" in here.
  13.  *
  14.  * When $form['empty'] is not set, a table will be rendered first. After that,
  15.  * all other form items will get rendered.
  16.  * If $form['empty'] is set, $form['header'] and $form['rows] will be deleted,
  17.  * and then all other form items will get rendered.
  18.  */
  19. function theme_mymodule_table_form($form) {
  20. $output = '';
  21. $header = array();
  22. $rows = array();
  23.  
  24. // We always want the heading to be rendered first.
  25. $output .= drupal_render($form['heading']);
  26.  
  27. if (!$form['empty']) {
  28. // Render $form['header'] as the header of the table.
  29. foreach ($form['header'] as $field) {
  30. $header[]['data'] = $field['#value'];
  31. }
  32. unset($form['header']);
  33.  
  34. // Render $form['rows'] as the rows of the table.
  35. foreach (element_children($form['rows']) as $i) {
  36. $row = array();
  37. foreach (element_children($form['rows'][$i]) as $key) {
  38. $row[]['data'] = drupal_render($form['rows'][$i][$key]);
  39. }
  40. $rows[] = $row;
  41. }
  42.  
  43. // Render the table.
  44. $output .= theme('table', $header, $rows, array('width' => '100%'));
  45. }
  46. else {
  47. // Delete the header and rows, just to be sure.
  48. unset($form['header']);
  49. unset($form['rows']);
  50.  
  51. // Render the empty message.
  52. $output .= drupal_render($form['empty']);
  53. }
  54.  
  55. // Render the remaining form items.
  56. $output .= drupal_render($form);
  57.  
  58. return $output;
  59. }

Report this snippet  

You need to login to post a comment.