Skip to content

Instantly share code, notes, and snippets.

@benclark
Last active December 18, 2015 16:09
Embed
What would you like to do?
Theme a series of form elements into a table (Drupal 6), with optional tabledrag.js support
<?php
/**
* Theme a series of form elements into a table (Drupal 6), with optional
* tabledrag.js support.
*/
function theme_D6MODULE_admin_table_form($form) {
$header = array();
$rows = array();
foreach (element_children($form) as $i) {
$row = array();
if (!isset($form[$i]['#sorted'])) {
uasort($form[$i], 'element_sort');
}
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,
'class' => !empty($form['#tabledrag']) ? 'draggable': '',
);
}
$attributes = isset($form['#attributes']) ? $form['#attributes'] : array();
if (!empty($form['#tabledrag'])) {
$attributes['id'] = isset($attributes['id']) ? $attributes['id'] . '-table' : 'tabledrag-table';
$tabledrag_field_class = !empty($form['#tabledrag_field_class']) ? $form['#tabledrag_field_class'] : 'tabledrag-field';
// Add basic tabledrag support to this table.
drupal_add_tabledrag($attributes['id'], 'order', 'sibling', $tabledrag_field_class);
}
return theme('table', $header, $rows, $attributes);
}
/**
* Implements hook_theme().
*/
function D6MODULE_theme() {
return array(
'D6MODULE_table_form' => array(
'arguments' => array('form' => NULL),
'file' => 'D6MODULE.admin.inc',
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment