Skip to content

Instantly share code, notes, and snippets.

@benclark
Created January 8, 2013 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benclark/4487413 to your computer and use it in GitHub Desktop.
Save benclark/4487413 to your computer and use it in GitHub Desktop.
Theme a series of form elements into a table (Drupal 7)
<?php
/**
* Theme a series of form elements into a table (Drupal 7).
*/
function theme_D7MODULE_table_form($variables) {
$form = $variables['form'];
$header = array();
$rows = array();
foreach (element_children($form) as $i) {
$row = array();
foreach (element_children($form[$i]) as $f) {
$this_element = &$form[$i][$f];
if (isset($this_element['#title'])) {
if (!isset($header[$f])) {
$header[$f] = $this_element['#title'];
}
unset($this_element['#title']);
if (isset($this_element['#label'])) {
$this_element['#title'] = $this_element['#label'];
}
$row[] = drupal_render($this_element);
}
}
$rows[] = array(
'data' => $row,
);
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
'empty' => t('No fields available in this context.'),
));
}
/**
* Implements hook_theme().
*/
function D7MODULE_theme() {
return array(
'D7MODULE_table_form' => array(
'render element' => 'form',
'file' => 'D7MODULE.admin.inc',
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment