Created
January 8, 2013 20:04
-
-
Save benclark/4487413 to your computer and use it in GitHub Desktop.
Theme a series of form elements into a table (Drupal 7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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