Return to Snippet

Revision: 4141
at October 28, 2007 15:19 by WimLeers


Updated Code
/**
 * 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 = '';
  $header = array();
  $rows = array();

  // 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'];
    }
    unset($form['header']);

    // Render $form['rows'] as the rows of the table.
    foreach (element_children($form['rows']) as $i) {
      $row = array();
      foreach (element_children($form['rows'][$i]) as $key) {
        $row[]['data'] = drupal_render($form['rows'][$i][$key]);
      }
      $rows[] = $row;
    }

    // Render the table.
    $output .= theme('table', $header, $rows, array('width' => '100%'));
  }
  else {
    // Delete the header and rows, just to be sure.
    unset($form['header']);
    unset($form['rows']);
    
    // Render the empty message.
    $output .= drupal_render($form['empty']);
  }

  // Render the remaining form items.
  $output .= drupal_render($form);

  return $output;
}

Revision: 4140
at October 28, 2007 15:18 by WimLeers


Initial Code
/**
 * 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_eightye_table_form($form) {
  $output = '';
  $header = array();
  $rows = array();

  // 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'];
    }
    unset($form['header']);

    // Render $form['rows'] as the rows of the table.
    foreach (element_children($form['rows']) as $i) {
      $row = array();
      foreach (element_children($form['rows'][$i]) as $key) {
        $row[]['data'] = drupal_render($form['rows'][$i][$key]);
      }
      $rows[] = $row;
    }

    // Render the table.
    $output .= theme('table', $header, $rows, array('width' => '100%'));
  }
  else {
    // Delete the header and rows, just to be sure.
    unset($form['header']);
    unset($form['rows']);
    
    // Render the empty message.
    $output .= drupal_render($form['empty']);
  }

  // Render the remaining form items.
  $output .= drupal_render($form);

  return $output;
}

Initial URL


Initial Description


Initial Title
table form themeing function

Initial Tags
textmate, drupal

Initial Language
PHP