/[drupal]/contributions/modules/elements/elements.module
ViewVC logotype

Contents of /contributions/modules/elements/elements.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Jan 31 00:20:16 2008 UTC (21 months, 4 weeks ago) by heine
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of elements module - This module intends to become a repository for advanced form elements, other developers can use. For now, it contains a tableselect element type that can be used to easily create tables as seen on admin/user/user. It also contains an imagebutton ripped from views, but this does not work satisfactorily. This module was commissioned by the NCRV (http://www.ncrv.nl)
1 <?php
2 // $Id$
3
4 /**
5 * Provide a list of additional form element-types.
6 *
7 * Implementation of hook_elements.
8 *
9 * @return array
10 * An array of element types.
11 */
12 function elements_elements() {
13 $types['tableselect'] = array('#input' => TRUE, '#advanced_select' => TRUE, '#multiple' => TRUE, '#process' => array('_elements_expand_tableselect' => array()));
14 $type['imagebutton'] = array('#input' => TRUE, '#button_type' => 'submit',);
15 return $types;
16 }
17
18
19 function theme_imagebutton($element) {
20 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";
21 }
22
23 function imagebutton_value() {
24 // null function guarantees default_value doesn't get moved to #value.
25 }
26
27 /**
28 * Create the right amount of checkboxes or radios to populate the table.
29 *
30 * @param unknown_type $element
31 * @return unknown
32 */
33 function _elements_expand_tableselect($element) {
34
35 if ($element['#multiple']) {
36 $value = is_array($element['#value']) ? $element['#value'] : array();
37 }
38 else {
39 // Advanced select is only possible if #multiple is true.
40 $element['#advanced_select'] = FALSE;
41 }
42
43 $element['#tree'] = TRUE;
44
45 if (count($element['#options']) > 0) {
46 if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
47 $element['#default_value'] = array();
48 }
49
50 foreach ($element['#options'] as $key => $choice) {
51 if ($element['#multiple']) {
52 $element[$key] = array('#type' => 'checkbox', '#processed' => TRUE, '#title' => '', '#return_value' => $key, '#default_value' => isset($value[$key]), '#attributes' => $element['#attributes']);
53 }
54 else {
55 $element[$key] = array('#type' => 'radio', '#title' => '', '#return_value' => $key, '#default_value' => ($element['#default_value'] == $key) ? $key : NULL, '#attributes' => $element['#attributes'], '#parents' => $element['#parents'], '#spawned' => TRUE);
56 }
57 }
58 }
59 return $element;
60 }
61
62 /**
63 * Generate a table with radios or checkboxes in the first column.
64 *
65 * @param unknown_type $element
66 * @return unknown
67 */
68 function theme_tableselect($element) {
69 $rows = array();
70 foreach ($element['#options'] as $key => $value) {
71 $row = array();
72 if ($element['#multiple']) {
73 $row[] = theme('checkbox', $element[$key]);
74 }
75 else {
76 $row[] = theme('radio', $element[$key]);
77 }
78 foreach ($element['#header'] as $fieldname => $title) {
79 $row[] = $element['#options'][$key][$fieldname];
80 }
81 $rows[] = $row;
82 }
83 $first_col = $element['#advanced_select'] ? array(theme('table_select_header_cell')) : array('');
84 $header = array_merge($first_col, $element['#header']);
85 return theme('table', $header, $rows);
86 }

  ViewVC Help
Powered by ViewVC 1.1.2