Returns HTML for a table with radio buttons or checkboxes.
$variables: An associative array containing:
$options = array(
array(
'title' => 'How to Learn Drupal',
'content_type' => 'Article',
'status' => 'published',
'#attributes' => array(
'class' => array(
'article-row',
),
),
),
array(
'title' => 'Privacy Policy',
'content_type' => 'Page',
'status' => 'published',
'#attributes' => array(
'class' => array(
'page-row',
),
),
),
);
$header = array(
'title' => t('Title'),
'content_type' => t('Content type'),
'status' => t('Status'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No content available.'),
);
function theme_tableselect($variables) {
$element = $variables['element'];
$rows = array();
$header = $element['#header'];
if (!empty($element['#options'])) {
// Generate a table row for each selectable item in #options.
foreach (element_children($element) as $key) {
$row = array();
$row['data'] = array();
if (isset($element['#options'][$key]['#attributes'])) {
$row += $element['#options'][$key]['#attributes'];
}
// Render the checkbox / radio element.
$row['data'][] = drupal_render($element[$key]);
// As theme_table only maps header and row columns by order, create the
// correct order by iterating over the header fields.
foreach ($element['#header'] as $fieldname => $title) {
// A row cell can span over multiple headers, which means less row cells
// than headers could be present.
if (isset($element['#options'][$key][$fieldname])) {
// A header can span over multiple cells and in this case the cells
// are passed in an array. The order of this array determines the
// order in which they are added.
if (!isset($element['#options'][$key][$fieldname]['data']) && is_array($element['#options'][$key][$fieldname])) {
foreach ($element['#options'][$key][$fieldname] as $cell) {
$row['data'][] = $cell;
}
}
else {
$row['data'][] = $element['#options'][$key][$fieldname];
}
}
}
$rows[] = $row;
}
// Add an empty header or a "Select all" checkbox to provide room for the
// checkboxes/radios in the first table column.
if ($element['#js_select']) {
// Add a "Select all" checkbox.
drupal_add_library('system', 'drupal.tableselect');
array_unshift($header, array(
'class' => array(
'select-all',
),
));
}
else {
// Add an empty header when radio buttons are displayed or a "Select all"
// checkbox is not desired.
array_unshift($header, '');
}
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
'empty' => $element['#empty'],
'attributes' => $element['#attributes'],
));
}