/[drupal]/contributions/docs/developer/examples/example_element.module
ViewVC logotype

Contents of /contributions/docs/developer/examples/example_element.module

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


Revision 1.6 - (show annotations) (download) (as text)
Sun Oct 12 08:52:36 2008 UTC (13 months, 2 weeks ago) by davereid
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +2 -2 lines
File MIME type: text/x-php
- Fix coding standards
1 <?php
2
3 // $Id: example_element.module,v 1.5 2008/09/15 21:57:06 davereid Exp $
4
5 /**
6 * @file
7 * This is an example demonstrating how a module can define custom form
8 * elements.
9 *
10 * Form elements are already familiar to anyone who uses Forms API. Examples
11 * of core form elements are 'textfield', 'checkbox' and 'fieldset'. Drupal
12 * utilizes hook_elements() to define these FAPI types, and this occurs in
13 * the core function system_elements().
14 *
15 * Each form element has a #type value that determines how it's treated by
16 * the Form API and how it's ultimately rendered into HTML. hook_elements()
17 * allows modules to define new element types, and tell the Form API what
18 * default values they should automatically be populated with.
19 *
20 * By implementing hook_elements in your own module, you can create custom
21 * form elements with their own properties, validation and theming.
22 *
23 * In this example, we will define a phone number field that is expanded
24 * into several text fields for area code, phone number and extention, each
25 * of which is validated.
26 */
27
28 /**
29 * Implementation of hook_menu().
30 *
31 * This just defines a page that we can use to test our form elements.
32 */
33 function example_element_menu() {
34 $items['example/element'] = array(
35 'title' => 'Example element demo',
36 'page callback' => 'drupal_get_form',
37 'page arguments' => array('example_element_demo_form'),
38 'access arguments' => array('access content'),
39 );
40 return $items;
41 }
42
43 /**
44 * Implementation of hook_elements().
45 */
46 function example_element_elements() {
47 $type['phonenumber'] = array(
48 '#input' => TRUE,
49 '#process' => array('example_element_phonenumber_expand'),
50 '#element_validate' => array('example_element_phonenumber_validate'),
51 '#default_value' => array('areacode' => '', 'number' => '', 'extension' => ''),
52 );
53 return $type;
54 }
55
56 /**
57 * Our process callback to expand the control.
58 */
59 function example_element_phonenumber_expand($element) {
60 $element['#tree'] = TRUE;
61
62 if (!isset($element['#value'])) {
63 $element['#value'] = array('areacode' => '', 'number' => '', 'extension' => '');
64 }
65
66 $element['areacode'] = array(
67 '#type' => 'textfield',
68 '#size' => 3,
69 '#maxlength' => 3,
70 '#value' => $element['#value']['areacode'],
71 '#prefix' => '(',
72 '#suffix' => ')',
73 );
74 $element['number'] = array(
75 '#type' => 'textfield',
76 '#size' => 8,
77 '#maxlength' => 8,
78 '#required' => TRUE,
79 '#value' => $element['#value']['number'],
80 );
81 $element['extension'] = array(
82 '#type' => 'textfield',
83 '#size' => 10,
84 '#maxlength' => 10,
85 '#prefix' => t('ext'),
86 '#value' => $element['#value']['extension'],
87 );
88
89 return $element;
90 }
91
92 /**
93 * Our element's validation function.
94 *
95 * We check that:
96 * - the area code is a three digit number
97 * - the number is numeric, with an optional dash
98 *
99 * Any problems are attached to the form element using form_error().
100 */
101 function example_element_phonenumber_validate($form, &$form_state) {
102 if (isset($form['#value']['areacode'])) {
103 if (0 == preg_match('/^\d{3}$/', $form['#value']['areacode'])) {
104 form_error($form['areacode'], t('The areacode is invalid.'));
105 }
106 }
107 if (isset($form['#value']['number'])) {
108 if (0 == preg_match('/^\d{3}-?\d{4}$/', $form['#value']['number'])) {
109 form_error($form['number'], t('The number is invalid.'));
110 }
111 }
112 return $form;
113
114 }
115
116 /**
117 * Implementation of hook_theme().
118 *
119 * This lets us tell Drupal about our theme functions and their arguments.
120 */
121 function example_element_theme() {
122 return array(
123 'phonenumber' => array(
124 'arguments' => array('element'),
125 ),
126 );
127 }
128
129 /**
130 * Theme function to format the output.
131 *
132 * We use the container-inline class so that all three of the HTML elements
133 * are placed next to each other, rather than on separate lines.
134 */
135 function theme_phonenumber($element) {
136 return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
137 }
138
139 /**
140 * This is a simple form to demonstrate how to use the phonenumber element we've
141 * defined.
142 */
143 function example_element_demo_form() {
144 $form['example_element_test_1'] = array(
145 '#type' => 'phonenumber',
146 '#title' => t('Phone number 1'),
147 '#default_value' => variable_get('example_element_test_1',
148 array('areacode' => '123', 'number' => '456-7890', 'extension' => '')
149 ),
150 '#description' => t('A phone number.'),
151 );
152
153 $form['example_element_test_2'] = array(
154 '#type' => 'phonenumber',
155 '#title' => t('Phone number 2'),
156 '#default_value' => variable_get('example_element_test_2',
157 array('areacode' => '', 'number' => '456-7890', 'extension' => '23')
158 ),
159 '#description' => t('Another phone number, a fax perhaps?'),
160 );
161
162 return system_settings_form($form);
163 }

  ViewVC Help
Powered by ViewVC 1.1.2