diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 8 | ||||
-rw-r--r-- | includes/form.inc | 111 | ||||
-rw-r--r-- | includes/tablesort.inc | 11 |
3 files changed, 124 insertions, 6 deletions
diff --git a/includes/common.inc b/includes/common.inc index 3e5d0c383..95bc9fc33 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3354,7 +3354,10 @@ function drupal_render(&$elements) { } $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : ''; $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : ''; - return $prefix . $content . $suffix; + $content = $prefix . $content . $suffix; + // Store the rendered content, so higher level elements can reuse it. + $elements['#content'] = $content; + return $content; } } @@ -3609,6 +3612,9 @@ function drupal_common_theme() { 'file' => array( 'arguments' => array('element' => NULL), ), + 'tableselect' => array( + 'arguments' => array('element' => NULL), + ), 'form_element' => array( 'arguments' => array('element' => NULL, 'value' => NULL), ), diff --git a/includes/form.inc b/includes/form.inc index 8ce4563cd..f5e1a61de 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -2037,6 +2037,117 @@ function form_process_checkboxes($element) { } /** + * Format a table with radio buttons or checkboxes. + * + * @param $element + * An associative array containing the properties and children of the + * tableselect element. + * Properties used: header, options, empty, js_select. + * + * @return + * A themed HTML string representing the table. + * + * @ingroup themeable + */ +function theme_tableselect($element) { + $rows = array(); + if (!empty($element['#options'])) { + // Generate a table row for each selectable item in #options. + foreach ($element['#options'] as $key => $value) { + $row = array(); + + // Render the checkbox / radio element. + $row[] = $element[$key]['#content']; + + // 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) { + $row[] = $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. + $first_col = $element['#js_select'] ? array(theme('table_select_header_cell')) : array(''); + $header = array_merge($first_col, $element['#header']); + } + else { + // If there are no selectable options, display the empty text over the + // entire width of the table. + $header = $element['#header']; + $rows[] = array(array('data' => $element['#empty'], 'colspan' => count($header))); + } + return theme('table', $header, $rows); +} + +/** + * Create the correct amount of checkbox or radio elements to populate the table. + * + * @param $element + * An associative array containing the properties and children of the + * tableselect element. + * + * @return + * The processed element. + */ +function form_process_tableselect($element) { + + if ($element['#multiple']) { + $value = is_array($element['#value']) ? $element['#value'] : array(); + } + else { + // Advanced selection behaviour make no sense for radios. + $element['#js_select'] = FALSE; + } + + $element['#tree'] = TRUE; + + if (count($element['#options']) > 0) { + if (!isset($element['#default_value']) || $element['#default_value'] === 0) { + $element['#default_value'] = array(); + } + + // Create a checkbox or radio for each item in #options in such a way that + // the value of the tableselect element behaves as if it had been of type + // checkboxes or radios. + foreach ($element['#options'] as $key => $choice) { + // Do not overwrite manually created children. + if (!isset($element[$key])) { + if ($element['#multiple']) { + $element[$key] = array( + '#type' => 'checkbox', + '#title' => '', + '#return_value' => $key, + '#default_value' => isset($value[$key]), + '#attributes' => $element['#attributes'], + '#ahah' => isset($element['#ahah']) ? $element['#ahah'] : NULL, + ); + } + else { + // Generate the parents as the autogenerator does, so we will have a + // unique id for each radio button. + $parents_for_id = array_merge($element['#parents'], array($key)); + $element[$key] = array( + '#type' => 'radio', + '#title' => '', + '#return_value' => $key, + '#default_value' => ($element['#default_value'] == $key) ? $key : NULL, + '#attributes' => $element['#attributes'], + '#parents' => $element['#parents'], + '#id' => form_clean_id('edit-' . implode('-', $parents_for_id)), + '#ahah' => isset($element['#ahah']) ? $element['#ahah'] : NULL, + ); + } + } + } + } + else { + $element['#value'] = array(); + } + return $element; +} + +/** * Theme a form submit button. * * @ingroup themeable diff --git a/includes/tablesort.inc b/includes/tablesort.inc index 3c2068e1b..36f01607f 100644 --- a/includes/tablesort.inc +++ b/includes/tablesort.inc @@ -165,13 +165,14 @@ function tablesort_get_order($headers) { return $default; } else { - // The first column specified is initial 'order by' field unless otherwise specified - if (is_array($headers[0])) { - $headers[0] += array('data' => NULL, 'field' => NULL); - return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']); + // The first column specified is the initial 'order by' field unless otherwise specified. + $first = current($headers); + if (is_array($first)) { + $first += array('data' => NULL, 'field' => NULL); + return array('name' => $first['data'], 'sql' => $first['field']); } else { - return array('name' => $headers[0]); + return array('name' => $first, 'sql' => ''); } } } |