function table_element_pre_render_table in Table Element 7
A #pre_render callback to transform children of an element into #rows.
Suitable for theme_table().
This function converts sub-elements of an element of #type 'table' to be suitable for theme_table():
- The first level of sub-elements are table rows. Only the #attributes property is taken into account.
- The second level of sub-elements is converted into columns for the corresponding first-level table row.
Simple example usage:
$form['table'] = array(
'#type' => 'table',
'#header' => array(
t('Title'),
array(
'data' => t('Operations'),
'colspan' => '1',
),
),
// Optionally, to add tableDrag support:
'#tabledrag' => array(
array(
'order',
'sibling',
'thing-weight',
),
),
);
foreach ($things as $row => $thing) {
$form['table'][$row]['#weight'] = $thing['weight'];
$form['table'][$row]['title'] = array(
'#type' => 'textfield',
'#default_value' => $thing['title'],
);
// Optionally, to add tableDrag support:
$form['table'][$row]['#attributes']['class'][] = 'draggable';
$form['table'][$row]['#cell']['colspan'] = '2';
$form['table'][$row]['#cell']['class'][] = 'td-element-class';
$form['table'][$row]['weight'] = array(
'#type' => 'textfield',
'#title' => t('Weight for @title', array(
'@title' => $thing['title'],
)),
'#title_display' => 'invisible',
'#size' => 4,
'#default_value' => $thing['weight'],
'#attributes' => array(
'class' => array(
'thing-weight',
),
),
);
// The amount of link columns should be identical to the 'colspan'
// attribute in #header above.
$form['table'][$row]['edit'] = array(
'#type' => 'link',
'#title' => t('Edit'),
'#href' => 'thing/' . $row . '/edit',
);
}
Parameters
array $element: A structured array containing two sub-levels of elements. Properties used:
- #tabledrag: The value is a list of arrays that are passed to drupal_add_tabledrag(). The HTML ID of the table is prepended to each set of arguments.
Return value
array An array of row information.
See also
1 string reference to 'table_element_pre_render_table'
- table_element_element_info in ./
table_element.module - Implements hook_element_info().
File
- ./
table_element.module, line 260 - Provides a table element for Drupal 7.
Code
function table_element_pre_render_table(array $element) {
foreach (element_children($element) as $first) {
$row = array(
'data' => array(),
);
// Apply attributes of first-level elements as table row attributes.
if (isset($element[$first]['#attributes'])) {
$row += $element[$first]['#attributes'];
}
// Turn second-level elements into table row columns.
foreach (element_children($element[$first]) as $second) {
$cell =& $element[$first][$second];
$cell += array(
// If an access is not configured then will assume that is
// granted. As Drupal does.
'#access' => TRUE,
// If attributes for table cell is not set then initialize
// an empty value for them.
'#cell' => array(),
);
// Element with granted access and without "value" type could
// be rendered as table cell.
if (!empty($cell['#access']) && !(isset($cell['#type']) && 'value' === $cell['#type'])) {
// Assign the element by reference, so any potential changes to the
// original element are taken over.
$row['data'][] = array(
'data' => &$cell,
) + $cell['#cell'];
}
}
$element['#rows'][] = $row;
}
// Take over $element['#id'] as HTML ID attribute, if not already set.
element_set_attributes($element, array(
'id',
));
// If the custom #tabledrag is set and there is a HTML ID, inject the table's
// HTML ID as first callback argument and attach the behavior.
if (!empty($element['#tabledrag']) && isset($element['#attributes']['id'])) {
foreach ($element['#tabledrag'] as &$args) {
array_unshift($args, $element['#attributes']['id']);
}
$element['#attached']['drupal_add_tabledrag'] = $element['#tabledrag'];
}
return $element;
}