elements.module in Elements 5
Same filename and directory in other branches
File
elements.moduleView source
<?php
/**
* Implements hook_elements().
*/
function elements_elements() {
$types['tableselect'] = array(
'#input' => TRUE,
'#js_select' => TRUE,
'#multiple' => TRUE,
'#process' => array(
'form_process_tableselect' => array(),
),
'#options' => array(),
'#empty' => '',
);
$types['imagebutton'] = array(
'#input' => TRUE,
'#button_type' => 'submit',
);
return $types;
}
/**
* Theme the imagebutton form element.
*
* @param array $element
*
* @return string
* HTML representation of the imagebutton.
*/
function theme_imagebutton($element) {
return '<input type="image" class="form-' . $element['#button_type'] . '" name="' . $element['#name'] . '" value="' . check_plain($element['#default_value']) . '" ' . drupal_attributes($element['#attributes']) . ' src="' . $element['#image'] . '" alt="' . $element['#title'] . '" title="' . $element['#title'] . "\" />\n";
}
/**
* Dummy function.
*/
function imagebutton_value() {
// NULL function guarantees default_value doesn't get moved to #value.
}
/**
* 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'],
'#processed' => TRUE,
);
}
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' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
'#spawned' => TRUE,
);
}
}
}
}
else {
$element['#value'] = array();
}
return $element;
}
/**
* Format a table with radio buttons or checkboxes.
*
* @param $variables
* An associative array containing:
* - element: An associative array containing the properties and children of
* the tableselect element.
* Each option in $variables['element']['#options'] can contain an array
* keyed by '#attributes' which is added to the row's HTML attributes.
* @see theme_table
* Properties used: header, options, empty, js_select.
*
* @return
* A themed HTML string representing the table.
*
* Example:
*
* @code
* $options = array();
* $options[0]['title'] = "A red row"
* $options[0]['#attributes'] = array ('class' => array('red-row'));
* $options[1]['title'] = "A blue row"
* $options[1]['#attributes'] = array ('class' => array('blue-row'));
*
* $form['myselector'] = array (
* '#type' => 'tableselect',
* '#title' => 'My Selector'
* '#options' => $options,
* );
* @ingroup themeable
*/
function theme_tableselect($element) {
$rows = array();
$header = $element['#header'];
if (!empty($element['#options'])) {
// Generate a table row for each selectable item in #options.
foreach ($element['#options'] as $key => $value) {
$row = array();
$row['data'] = array();
if (isset($value['#attributes'])) {
$row += $value['#attributes'];
}
// Render the checkbox / radio element.
unset($element[$key]['#printed']);
// NEW
$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) {
$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.
$first_col = $element['#js_select'] ? array(
theme('table_select_header_cell'),
) : array(
'',
);
$header = array_merge($first_col, $element['#header']);
}
elseif ($element['#empty']) {
// If there are no selectable options, display the empty text over the
// entire width of the table.
$rows[] = array(
array(
'data' => $element['#empty'],
'colspan' => count($header),
),
);
}
return theme('table', $header, $rows);
}
Functions
Name | Description |
---|---|
elements_elements | Implements hook_elements(). |
form_process_tableselect | Create the correct amount of checkbox or radio elements to populate the table. |
imagebutton_value | Dummy function. |
theme_imagebutton | Theme the imagebutton form element. |
theme_tableselect | Format a table with radio buttons or checkboxes. |