1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Defines selection, check box and radio button widgets for text and numeric fields.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_help().
|
10
|
*/
|
11
|
function options_help($path, $arg) {
|
12
|
switch ($path) {
|
13
|
case 'admin/help#options':
|
14
|
$output = '';
|
15
|
$output .= '<h3>' . t('About') . '</h3>';
|
16
|
$output .= '<p>' . t('The Options module defines checkbox, selection, and other input widgets for the Field module. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
|
17
|
return $output;
|
18
|
}
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* Implements hook_theme().
|
23
|
*/
|
24
|
function options_theme() {
|
25
|
return array(
|
26
|
'options_none' => array(
|
27
|
'variables' => array('instance' => NULL, 'option' => NULL),
|
28
|
),
|
29
|
);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Implements hook_field_widget_info().
|
34
|
*
|
35
|
* Field type modules willing to use those widgets should:
|
36
|
* - Use hook_field_widget_info_alter() to append their field own types to the
|
37
|
* list of types supported by the widgets,
|
38
|
* - Implement hook_options_list() to provide the list of options.
|
39
|
* See list.module.
|
40
|
*/
|
41
|
function options_field_widget_info() {
|
42
|
return array(
|
43
|
'options_select' => array(
|
44
|
'label' => t('Select list'),
|
45
|
'field types' => array(),
|
46
|
'behaviors' => array(
|
47
|
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
48
|
),
|
49
|
),
|
50
|
'options_buttons' => array(
|
51
|
'label' => t('Check boxes/radio buttons'),
|
52
|
'field types' => array(),
|
53
|
'behaviors' => array(
|
54
|
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
55
|
),
|
56
|
),
|
57
|
'options_onoff' => array(
|
58
|
'label' => t('Single on/off checkbox'),
|
59
|
'field types' => array(),
|
60
|
'behaviors' => array(
|
61
|
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
62
|
),
|
63
|
'settings' => array('display_label' => 0),
|
64
|
),
|
65
|
);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Implements hook_field_widget_form().
|
70
|
*/
|
71
|
function options_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
72
|
// Abstract over the actual field columns, to allow different field types to
|
73
|
// reuse those widgets.
|
74
|
$value_key = key($field['columns']);
|
75
|
|
76
|
$type = str_replace('options_', '', $instance['widget']['type']);
|
77
|
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
|
78
|
$required = $element['#required'];
|
79
|
$has_value = isset($items[0][$value_key]);
|
80
|
$properties = _options_properties($type, $multiple, $required, $has_value);
|
81
|
|
82
|
$entity_type = $element['#entity_type'];
|
83
|
$entity = $element['#entity'];
|
84
|
|
85
|
// Prepare the list of options.
|
86
|
$options = _options_get_options($field, $instance, $properties, $entity_type, $entity);
|
87
|
|
88
|
// Put current field values in shape.
|
89
|
$default_value = _options_storage_to_form($items, $options, $value_key, $properties);
|
90
|
|
91
|
switch ($type) {
|
92
|
case 'select':
|
93
|
$element += array(
|
94
|
'#type' => 'select',
|
95
|
'#default_value' => $default_value,
|
96
|
// Do not display a 'multiple' select box if there is only one option.
|
97
|
'#multiple' => $multiple && count($options) > 1,
|
98
|
'#options' => $options,
|
99
|
);
|
100
|
break;
|
101
|
|
102
|
case 'buttons':
|
103
|
// If required and there is one single option, preselect it.
|
104
|
if ($required && count($options) == 1) {
|
105
|
reset($options);
|
106
|
$default_value = array(key($options));
|
107
|
}
|
108
|
|
109
|
// If this is a single-value field, take the first default value, or
|
110
|
// default to NULL so that the form element is properly recognized as
|
111
|
// not having a default value.
|
112
|
if (!$multiple) {
|
113
|
$default_value = $default_value ? reset($default_value) : NULL;
|
114
|
}
|
115
|
|
116
|
$element += array(
|
117
|
'#type' => $multiple ? 'checkboxes' : 'radios',
|
118
|
// Radio buttons need a scalar value.
|
119
|
'#default_value' => $default_value,
|
120
|
'#options' => $options,
|
121
|
);
|
122
|
break;
|
123
|
|
124
|
case 'onoff':
|
125
|
$keys = array_keys($options);
|
126
|
$off_value = array_shift($keys);
|
127
|
$on_value = array_shift($keys);
|
128
|
$element += array(
|
129
|
'#type' => 'checkbox',
|
130
|
'#default_value' => (isset($default_value[0]) && $default_value[0] == $on_value) ? 1 : 0,
|
131
|
'#on_value' => $on_value,
|
132
|
'#off_value' => $off_value,
|
133
|
);
|
134
|
// Override the title from the incoming $element.
|
135
|
$element['#title'] = isset($options[$on_value]) ? $options[$on_value] : '';
|
136
|
|
137
|
if ($instance['widget']['settings']['display_label']) {
|
138
|
$element['#title'] = $instance['label'];
|
139
|
}
|
140
|
break;
|
141
|
}
|
142
|
|
143
|
$element += array(
|
144
|
'#value_key' => $value_key,
|
145
|
'#element_validate' => array('options_field_widget_validate'),
|
146
|
'#properties' => $properties,
|
147
|
);
|
148
|
|
149
|
return $element;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Implements hook_field_widget_settings_form().
|
154
|
*/
|
155
|
function options_field_widget_settings_form($field, $instance) {
|
156
|
$form = array();
|
157
|
if ($instance['widget']['type'] == 'options_onoff') {
|
158
|
$form['display_label'] = array(
|
159
|
'#type' => 'checkbox',
|
160
|
'#title' => t('Use field label instead of the "On value" as label'),
|
161
|
'#default_value' => $instance['widget']['settings']['display_label'],
|
162
|
'#weight' => -1,
|
163
|
);
|
164
|
}
|
165
|
return $form;
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Form element validation handler for options element.
|
170
|
*/
|
171
|
function options_field_widget_validate($element, &$form_state) {
|
172
|
if ($element['#required'] && $element['#value'] == '_none') {
|
173
|
form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
|
174
|
}
|
175
|
// Transpose selections from field => delta to delta => field, turning
|
176
|
// multiple selected options into multiple parent elements.
|
177
|
$items = _options_form_to_storage($element);
|
178
|
form_set_value($element, $items, $form_state);
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Describes the preparation steps required by each widget.
|
183
|
*/
|
184
|
function _options_properties($type, $multiple, $required, $has_value) {
|
185
|
$base = array(
|
186
|
'filter_xss' => FALSE,
|
187
|
'strip_tags' => FALSE,
|
188
|
'strip_tags_and_unescape' => FALSE,
|
189
|
'empty_option' => FALSE,
|
190
|
'optgroups' => FALSE,
|
191
|
);
|
192
|
|
193
|
$properties = array();
|
194
|
|
195
|
switch ($type) {
|
196
|
case 'select':
|
197
|
$properties = array(
|
198
|
// Select boxes do not support any HTML tag.
|
199
|
'strip_tags_and_unescape' => TRUE,
|
200
|
'optgroups' => TRUE,
|
201
|
);
|
202
|
if ($multiple) {
|
203
|
// Multiple select: add a 'none' option for non-required fields.
|
204
|
if (!$required) {
|
205
|
$properties['empty_option'] = 'option_none';
|
206
|
}
|
207
|
}
|
208
|
else {
|
209
|
// Single select: add a 'none' option for non-required fields,
|
210
|
// and a 'select a value' option for required fields that do not come
|
211
|
// with a value selected.
|
212
|
if (!$required) {
|
213
|
$properties['empty_option'] = 'option_none';
|
214
|
}
|
215
|
elseif (!$has_value) {
|
216
|
$properties['empty_option'] = 'option_select';
|
217
|
}
|
218
|
}
|
219
|
break;
|
220
|
|
221
|
case 'buttons':
|
222
|
$properties = array(
|
223
|
'filter_xss' => TRUE,
|
224
|
);
|
225
|
// Add a 'none' option for non-required radio buttons.
|
226
|
if (!$required && !$multiple) {
|
227
|
$properties['empty_option'] = 'option_none';
|
228
|
}
|
229
|
break;
|
230
|
|
231
|
case 'onoff':
|
232
|
$properties = array(
|
233
|
'filter_xss' => TRUE,
|
234
|
);
|
235
|
break;
|
236
|
}
|
237
|
|
238
|
return $properties + $base;
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Collects the options for a field.
|
243
|
*/
|
244
|
function _options_get_options($field, $instance, $properties, $entity_type, $entity) {
|
245
|
// Get the list of options.
|
246
|
$options = (array) module_invoke($field['module'], 'options_list', $field, $instance, $entity_type, $entity);
|
247
|
|
248
|
// Sanitize the options.
|
249
|
_options_prepare_options($options, $properties);
|
250
|
|
251
|
if (!$properties['optgroups']) {
|
252
|
$options = options_array_flatten($options);
|
253
|
}
|
254
|
|
255
|
if ($properties['empty_option']) {
|
256
|
$label = theme('options_none', array('instance' => $instance, 'option' => $properties['empty_option']));
|
257
|
$options = array('_none' => $label) + $options;
|
258
|
}
|
259
|
|
260
|
return $options;
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Sanitizes the options.
|
265
|
*
|
266
|
* The function is recursive to support optgroups.
|
267
|
*/
|
268
|
function _options_prepare_options(&$options, $properties) {
|
269
|
foreach ($options as $value => $label) {
|
270
|
// Recurse for optgroups.
|
271
|
if (is_array($label)) {
|
272
|
_options_prepare_options($options[$value], $properties);
|
273
|
}
|
274
|
else {
|
275
|
// The 'strip_tags' option is deprecated. Use 'strip_tags_and_unescape'
|
276
|
// when plain text is required (and where the output will be run through
|
277
|
// check_plain() before being inserted back into HTML) or 'filter_xss'
|
278
|
// when HTML is required.
|
279
|
if ($properties['strip_tags']) {
|
280
|
$options[$value] = strip_tags($label);
|
281
|
}
|
282
|
if ($properties['strip_tags_and_unescape']) {
|
283
|
$options[$value] = decode_entities(strip_tags($label));
|
284
|
}
|
285
|
if ($properties['filter_xss']) {
|
286
|
$options[$value] = field_filter_xss($label);
|
287
|
}
|
288
|
}
|
289
|
}
|
290
|
}
|
291
|
|
292
|
/**
|
293
|
* Transforms stored field values into the format the widgets need.
|
294
|
*/
|
295
|
function _options_storage_to_form($items, $options, $column, $properties) {
|
296
|
$items_transposed = options_array_transpose($items);
|
297
|
$values = (isset($items_transposed[$column]) && is_array($items_transposed[$column])) ? $items_transposed[$column] : array();
|
298
|
|
299
|
// Discard values that are not in the current list of options. Flatten the
|
300
|
// array if needed.
|
301
|
if ($properties['optgroups']) {
|
302
|
$options = options_array_flatten($options);
|
303
|
}
|
304
|
$values = array_values(array_intersect($values, array_keys($options)));
|
305
|
return $values;
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Transforms submitted form values into field storage format.
|
310
|
*/
|
311
|
function _options_form_to_storage($element) {
|
312
|
$values = array_values((array) $element['#value']);
|
313
|
$properties = $element['#properties'];
|
314
|
|
315
|
// On/off checkbox: transform '0 / 1' into the 'on / off' values.
|
316
|
if ($element['#type'] == 'checkbox') {
|
317
|
$values = array($values[0] ? $element['#on_value'] : $element['#off_value']);
|
318
|
}
|
319
|
|
320
|
// Filter out the 'none' option. Use a strict comparison, because
|
321
|
// 0 == 'any string'.
|
322
|
if ($properties['empty_option']) {
|
323
|
$index = array_search('_none', $values, TRUE);
|
324
|
if ($index !== FALSE) {
|
325
|
unset($values[$index]);
|
326
|
}
|
327
|
}
|
328
|
|
329
|
// Make sure we populate at least an empty value.
|
330
|
if (empty($values)) {
|
331
|
$values = array(NULL);
|
332
|
}
|
333
|
|
334
|
$result = options_array_transpose(array($element['#value_key'] => $values));
|
335
|
return $result;
|
336
|
}
|
337
|
|
338
|
/**
|
339
|
* Manipulates a 2D array to reverse rows and columns.
|
340
|
*
|
341
|
* The default data storage for fields is delta first, column names second.
|
342
|
* This is sometimes inconvenient for field modules, so this function can be
|
343
|
* used to present the data in an alternate format.
|
344
|
*
|
345
|
* @param $array
|
346
|
* The array to be transposed. It must be at least two-dimensional, and
|
347
|
* the subarrays must all have the same keys or behavior is undefined.
|
348
|
* @return
|
349
|
* The transposed array.
|
350
|
*/
|
351
|
function options_array_transpose($array) {
|
352
|
$result = array();
|
353
|
if (is_array($array)) {
|
354
|
foreach ($array as $key1 => $value1) {
|
355
|
if (is_array($value1)) {
|
356
|
foreach ($value1 as $key2 => $value2) {
|
357
|
if (!isset($result[$key2])) {
|
358
|
$result[$key2] = array();
|
359
|
}
|
360
|
$result[$key2][$key1] = $value2;
|
361
|
}
|
362
|
}
|
363
|
}
|
364
|
}
|
365
|
return $result;
|
366
|
}
|
367
|
|
368
|
/**
|
369
|
* Flattens an array of allowed values.
|
370
|
*
|
371
|
* @param $array
|
372
|
* A single or multidimensional array.
|
373
|
* @return
|
374
|
* A flattened array.
|
375
|
*/
|
376
|
function options_array_flatten($array) {
|
377
|
$result = array();
|
378
|
if (is_array($array)) {
|
379
|
foreach ($array as $key => $value) {
|
380
|
if (is_array($value)) {
|
381
|
$result += options_array_flatten($value);
|
382
|
}
|
383
|
else {
|
384
|
$result[$key] = $value;
|
385
|
}
|
386
|
}
|
387
|
}
|
388
|
return $result;
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* Implements hook_field_widget_error().
|
393
|
*/
|
394
|
function options_field_widget_error($element, $error, $form, &$form_state) {
|
395
|
form_error($element, $error['message']);
|
396
|
}
|
397
|
|
398
|
/**
|
399
|
* Returns HTML for the label for the empty value for options that are not required.
|
400
|
*
|
401
|
* The default theme will display N/A for a radio list and '- None -' for a select.
|
402
|
*
|
403
|
* @param $variables
|
404
|
* An associative array containing:
|
405
|
* - instance: An array representing the widget requesting the options.
|
406
|
*
|
407
|
* @ingroup themeable
|
408
|
*/
|
409
|
function theme_options_none($variables) {
|
410
|
$instance = $variables['instance'];
|
411
|
$option = $variables['option'];
|
412
|
|
413
|
$output = '';
|
414
|
switch ($instance['widget']['type']) {
|
415
|
case 'options_buttons':
|
416
|
$output = t('N/A');
|
417
|
break;
|
418
|
|
419
|
case 'options_select':
|
420
|
$output = ($option == 'option_none' ? t('- None -') : t('- Select a value -'));
|
421
|
break;
|
422
|
}
|
423
|
|
424
|
return $output;
|
425
|
}
|