5 * Webform module multiple select component.
9 * Implements _webform_defaults_component().
11 function _webform_defaults_select() {
25 'other_option' => NULL,
26 'other_text' => t('Other...'),
29 'description_above' => FALSE,
30 'custom_keys' => FALSE,
31 'options_source' => '',
39 * Implements _webform_theme_component().
41 function _webform_theme_select() {
43 'webform_display_select' => array(
44 'render element' => 'element',
45 'file' => 'components/select.inc',
51 * Implements _webform_edit_component().
53 function _webform_edit_select($component) {
57 drupal_get_path('module', 'webform') . '/js/select-admin.js' => array('preprocess' => FALSE),
58 array('data' => array('webform' => array('selectOptionsUrl' => url('webform/ajax/options/' . $component['nid']))), 'type' => 'setting'),
63 // Default component if nested under a grid.
64 if (!isset($component['cid']) && $component['pid'] &&
65 ($node = node_load($component['nid'])) && ($parent = $node->webform['components'][$component['pid']]) &&
66 $parent['type'] == 'grid') {
67 $component['value'] = $parent['value'];
68 $component['extra']['items'] = $parent['extra']['options'];
69 $component['required'] = $parent['required'];
73 if ($info = _webform_select_options_info()) {
74 $options = array('' => t('None'));
75 foreach ($info as $name => $source) {
76 $options[$name] = $source['title'];
79 $other['options_source'] = array(
80 '#title' => t('Load a pre-built option list'),
82 '#options' => $options,
83 '#default_value' => $component['extra']['options_source'],
84 '#description' => t('Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.'),
85 '#parents' => array('extra', 'options_source'),
90 if (module_exists('select_or_other')) {
91 $other['other_option'] = array(
92 '#type' => 'checkbox',
93 '#title' => t('Allow "Other..." option'),
94 '#default_value' => $component['extra']['other_option'],
95 '#description' => t('Check this option if you want to allow users to enter an option not on the list.'),
96 '#parents' => array('extra', 'other_option'),
97 '#attributes' => array('class' => array('other-option-checkbox')),
100 $other['other_text'] = array(
101 '#type' => 'textfield',
102 '#title' => t('Text for "Other..." option'),
103 '#default_value' => $component['extra']['other_text'],
104 '#description' => t('If allowing other options, enter text to be used for other-enabling option.'),
105 '#parents' => array('extra', 'other_text'),
109 ':input.other-option-checkbox' => array('checked' => TRUE),
115 if (module_exists('options_element')) {
116 $options = _webform_select_options($component, FALSE, FALSE);
118 $form['items'] = array(
119 '#type' => 'fieldset',
120 '#title' => t('Options'),
121 '#collapsible' => TRUE,
122 '#attributes' => array('class' => array('webform-options-element')),
123 '#element_validate' => array('_webform_edit_validate_options'),
127 $form['items']['options'] = array(
128 '#type' => 'options',
130 '#optgroups' => TRUE,
131 '#multiple' => $component['extra']['multiple'],
132 '#multiple_toggle' => t('Multiple'),
133 '#default_value' => $component['value'],
134 '#options' => $options,
135 '#options_readonly' => !empty($component['extra']['options_source']),
136 '#key_type' => 'mixed',
137 '#key_type_toggle' => t('Customize keys (Advanced)'),
138 '#key_type_toggled' => $component['extra']['custom_keys'],
139 '#default_value_pattern' => '^%.+\[.+\]$',
143 $form['items']['options']['option_settings'] = $other;
146 $form['extra']['items'] = array(
147 '#type' => 'textarea',
148 '#title' => t('Options'),
149 '#default_value' => $component['extra']['items'],
150 '#description' => t('<strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys. One option per line. Option groups may be specified with <Group Name>. <> can be used to insert items at the root of the menu after specifying a group.') . ' ' . theme('webform_token_help'),
156 '#element_validate' => array('_webform_edit_validate_select'),
159 if (!empty($component['extra']['options_source'])) {
160 $form['extra']['items']['#attributes'] = array('readonly' => 'readonly');
163 $form['extra'] = array_merge($form['extra'], $other);
164 $form['value'] = array(
165 '#type' => 'textfield',
166 '#title' => t('Default value'),
167 '#default_value' => $component['value'],
168 '#description' => t('The default value of the field identified by its key. For multiple selects use commas to separate multiple defaults.') . ' ' . theme('webform_token_help'),
170 '#maxlength' => 1024,
173 $form['extra']['multiple'] = array(
174 '#type' => 'checkbox',
175 '#title' => t('Multiple'),
176 '#default_value' => $component['extra']['multiple'],
177 '#description' => t('Check this option if the user should be allowed to choose multiple values.'),
182 $form['display']['aslist'] = array(
183 '#type' => 'checkbox',
184 '#title' => t('Listbox'),
185 '#default_value' => $component['extra']['aslist'],
186 '#description' => t('Check this option if you want the select component to be displayed as a select list box instead of radio buttons or checkboxes. Option groups (nested options) are only supported with listbox components.'),
187 '#parents' => array('extra', 'aslist'),
189 $form['display']['empty_option'] = array(
190 '#type' => 'textfield',
191 '#title' => t('Empty option'),
192 '#default_value' => $component['extra']['empty_option'],
195 '#description' => t('The list item to show when no default is provided. Leave blank for "- None -" or "- Select -".'),
196 '#parents' => array('extra', 'empty_option'),
199 ':input[name="extra[aslist]"]' => array('checked' => TRUE),
200 ':input[name="extra[multiple]"]' => array('checked' => FALSE),
201 ':input[name="value"]' => array('filled' => FALSE),
205 $form['display']['optrand'] = array(
206 '#type' => 'checkbox',
207 '#title' => t('Randomize options'),
208 '#default_value' => $component['extra']['optrand'],
209 '#description' => t('Randomizes the order of the options when they are displayed in the form.'),
210 '#parents' => array('extra', 'optrand'),
217 * Element validation callback. Ensure keys are not duplicated.
219 function _webform_edit_validate_select($element, &$form_state) {
220 // Check for duplicate key values to prevent unexpected data loss. Require
221 // all options to include a safe_key.
222 if (!empty($element['#value'])) {
223 $lines = explode("\n", trim($element['#value']));
224 $existing_keys = array();
225 $duplicate_keys = array();
226 $missing_keys = array();
227 $long_keys = array();
229 foreach ($lines as $line) {
232 if (preg_match('/^\<([^>]*)\>$/', $line, $matches)) {
233 $group = $matches[1];
234 $key = NULL; // No need to store group names.
236 elseif (preg_match('/^([^|]*)\|(.*)$/', $line, $matches)) {
238 if (strlen($key) > 128) {
243 $missing_keys[] = $line;
247 if (isset($existing_keys[$group][$key])) {
248 $duplicate_keys[$key] = $key;
251 $existing_keys[$group][$key] = $key;
256 if (!empty($missing_keys)) {
257 form_error($element, t('Every option must have a key specified. Specify each option as "safe_key|Some readable option".'));
260 if (!empty($long_keys)) {
261 form_error($element, t('Option keys must be less than 128 characters. The following keys exceed this limit:') . theme('item_list', $long_keys));
264 if (!empty($duplicate_keys)) {
265 form_error($element, t('Options within the select list must be unique. The following keys have been used multiple times:') . theme('item_list', array('items' => $duplicate_keys)));
268 // Set the listbox option if needed.
269 if (empty($missing_keys) && empty($long_keys) && empty($duplicate_keys)) {
270 $options = _webform_select_options_from_text($element['#value']);
271 _webform_edit_validate_set_aslist($options, $form_state);
279 * Set the appropriate webform values when using the options element module.
281 function _webform_edit_validate_options($element, &$form_state) {
282 $key = end($element['#parents']);
283 $element_options = $form_state['values'][$key]['options'];
284 unset($form_state['values'][$key]);
286 $form_state['values']['extra'][$key] = form_options_to_text($element_options['options'], 'custom');
288 // Options saved for select components.
289 if ($key == 'items') {
290 $form_state['values']['extra']['multiple'] = $element_options['multiple'];
291 $form_state['values']['extra']['custom_keys'] = $element_options['custom_keys'];
292 $form_state['values']['value'] = is_array($element_options['default_value']) ? implode(', ', $element_options['default_value']) : $element_options['default_value'];
294 // Set the listbox option if needed.
295 _webform_edit_validate_set_aslist($element_options['options'], $form_state);
297 // Options saved for grid components.
299 $form_state['values']['extra']['custom_' . rtrim($key, 's') . '_keys'] = $element_options['custom_keys'];
300 // There is only one 'value', but grids have two options widgets, one for questions and one for options.
301 // Only options have a default 'value'. Note that multiple selection is now allowed for grid options.
302 if ($key == 'options') {
303 $form_state['values']['value'] = $element_options['default_value'];
309 * Ensure "aslist" is used for option groups. Called from options validations.
311 function _webform_edit_validate_set_aslist($options, &$form_state) {
312 if (empty($form_state['values']['extra']['aslist']) && !empty($options)) {
313 foreach ($options as $option) {
314 if (is_array($option)) {
315 $form_state['values']['extra']['aslist'] = 1;
316 drupal_set_message(t('The component %name has automatically been set to display as a listbox in order to support option groups.', array('%name' => $form_state['values']['name'])), 'warning');
324 * Implements _webform_render_component().
326 function _webform_render_select($component, $value = NULL, $filter = TRUE, $submission = NULL) {
327 $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
330 '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
331 '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
332 '#required' => $component['required'],
333 '#weight' => $component['weight'],
334 '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
335 '#theme_wrappers' => array('webform_element'),
336 '#translatable' => array('title', 'description', 'options'),
339 // Prevent double-wrapping of radios and checkboxes.
340 if (!$component['extra']['aslist']) {
341 $element['#pre_render'] = array();
344 // Convert the user-entered options list into an array.
345 $default_value = $filter ? webform_replace_tokens($component['value'], $node) : $component['value'];
346 $options = _webform_select_options($component, !$component['extra']['aslist'], $filter);
347 if (empty($options)) {
348 // Make element inaccessible if there are no options as there is no point in showing it.
349 $element['#access'] = FALSE;
352 if ($component['extra']['optrand']) {
353 _webform_shuffle_options($options);
356 // Add HTML5 required attribute, if needed and possible (not working on more than one checkboxes).
357 if ($component['required'] && ($component['extra']['aslist'] || !$component['extra']['multiple'] || count($options) == 1)) {
358 $element['#attributes']['required'] = 'required';
361 // Add default options if using a select list with no default. This trigger's
362 // Drupal 7's adding of the option for us. See @form_process_select().
363 if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
364 $element['#empty_value'] = '';
365 if (strlen($component['extra']['empty_option'])) {
366 $element['#empty_option'] = $component['extra']['empty_option'];
367 $element['#translatable'][] = 'empty_option';
371 // Set the component options.
372 $element['#options'] = $options;
374 // Use the component's default value if the component is currently empty.
375 if (!isset($value)) {
376 // The default for multiple selects is a comma-delimited list, without white-space or empty entries.
377 $value = $component['extra']['multiple'] ? array_filter(array_map('trim', explode(',', $default_value)), 'strlen') : $default_value;
380 // Convert all values into an array; component may now be single but was previously multiple, or vice-versa
381 $value = (array)$value;
383 // Set the default value. Note: "No choice" is stored as an empty string,
384 // which will match a 0 key for radios; NULL is used to avoid unintentional
385 // defaulting to the 0 option.
386 if ($component['extra']['multiple']) {
387 // Set the value as an array.
388 $element['#default_value'] = array();
389 foreach ($value as $option_value) {
390 $element['#default_value'][] = $option_value === '' ? NULL : $option_value;
394 // Set the value as a single string.
395 $option_value = reset($value);
396 $element['#default_value'] = $option_value === '' ? NULL : $option_value;
399 if ($component['extra']['other_option'] && module_exists('select_or_other')) {
400 // Set display as a select_or_other element:
401 $element['#type'] = 'select_or_other';
402 $element['#other'] = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
403 $element['#translatable'][] = 'other';
404 $element['#other_title'] = $element['#title'] . ' ' . $element['#other'];
405 $element['#other_title_display'] = 'invisible';
406 $element['#other_unknown_defaults'] = 'other';
407 $element['#other_delimiter'] = ', ';
408 // Merge in Webform's #process function for Select or other.
409 $element['#process'] = array_merge(element_info_property('select_or_other', '#process'), array('webform_expand_select_or_other'));
410 // Inherit select_or_other settings or set defaults.
411 $element['#disabled'] = isset($component['extra']['#disabled']) ? $component['extra']['#disabled'] : element_info_property('select_or_other', 'disabled');
412 $element['#size'] = isset($component['extra']['#size']) ? $component['extra']['#size'] : element_info_property('select_or_other', 'size');
414 if ($component['extra']['multiple']) {
415 $element['#multiple'] = TRUE;
416 $element['#select_type'] = 'checkboxes';
419 $element['#multiple'] = FALSE;
420 $element['#select_type'] = 'radios';
422 if ($component['extra']['aslist']) {
423 $element['#select_type'] = 'select';
426 elseif ($component['extra']['aslist']) {
427 // Set display as a select list:
428 $element['#type'] = 'select';
429 if ($component['extra']['multiple']) {
430 $element['#size'] = 4;
431 $element['#multiple'] = TRUE;
435 if ($component['extra']['multiple']) {
436 // Set display as a checkbox set.
437 $element['#type'] = 'checkboxes';
438 $element['#theme_wrappers'] = array_merge(array('checkboxes'), $element['#theme_wrappers']);
439 $element['#process'] = array_merge(element_info_property('checkboxes', '#process'), array('webform_expand_select_ids'));
442 // Set display as a radio set.
443 $element['#type'] = 'radios';
444 $element['#theme_wrappers'] = array_merge(array('radios'), $element['#theme_wrappers']);
445 $element['#process'] = array_merge(element_info_property('radios', '#process'), array('webform_expand_select_ids'));
453 * Process function to ensure select_or_other elements validate properly.
455 function webform_expand_select_or_other($element) {
456 // Disable validation for back-button and save draft.
457 $element['select']['#validated'] = TRUE;
458 $element['select']['#webform_validated'] = FALSE;
460 $element['other']['#validated'] = TRUE;
461 $element['other']['#webform_validated'] = FALSE;
463 // The Drupal FAPI does not support #title_display inline so we need to move
464 // to a supported value here to be compatible with select_or_other.
465 $element['select']['#title_display'] = $element['#title_display'] === 'inline' ? 'before' : $element['#title_display'];
467 // If the default value contains "select_or_other" (the key of the select
468 // element for the "other..." choice), discard it and set the "other" value.
469 if (is_array($element['#default_value']) && in_array('select_or_other', $element['#default_value'])) {
470 $key = array_search('select_or_other', $element['#default_value']);
471 unset($element['#default_value'][$key]);
472 $element['#default_value'] = array_values($element['#default_value']);
473 $element['other']['#default_value'] = implode(', ', $element['#default_value']);
476 // Sanitize the options in Select or other check boxes and radio buttons.
477 if ($element['#select_type'] == 'checkboxes' || $element['#select_type'] == 'radios') {
478 $element['select']['#process'] = array_merge(element_info_property($element['#select_type'], '#process'), array('webform_expand_select_ids'));
485 * FAPI process function to rename IDs attached to checkboxes and radios.
487 function webform_expand_select_ids($element) {
488 $id = $element['#id'] = str_replace('_', '-', _webform_safe_name(strip_tags($element['#id'])));
490 foreach (element_children($element) as $key) {
492 // Convert the #id for each child to a safe name, regardless of key.
493 $element[$key]['#id'] = $id . '-' . $delta;
495 // Prevent scripts or CSS in the labels for each checkbox or radio.
496 $element[$key]['#title'] = isset($element[$key]['#title']) ? webform_filter_xss($element[$key]['#title']) : '';
502 * Implements _webform_display_component().
504 function _webform_display_select($component, $value, $format = 'html', $submission = array()) {
505 // Sort values by numeric key. These may be in alphabetic order from the database query,
506 // which is not numeric order for keys '10' and higher.
507 $value = (array) $value;
508 ksort($value, SORT_NUMERIC);
510 '#title' => $component['name'],
511 '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
512 '#weight' => $component['weight'],
513 '#multiple' => $component['extra']['multiple'],
514 '#theme' => 'webform_display_select',
515 '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
516 '#format' => $format,
517 '#options' => _webform_select_options($component, !$component['extra']['aslist']),
519 '#translatable' => array('title', 'options'),
524 * Implements _webform_submit_component().
526 * Handle select or other... modifications and convert FAPI 0/1 values into
527 * something saveable.
529 function _webform_submit_select($component, $value) {
530 if (module_exists('select_or_other') && $component['extra']['other_option'] && is_array($value) && count($value) == 2 && isset($value['select'])) {
531 if (is_array($value['select']) && isset($value['select']['select_or_other'])) {
532 unset($value['select']['select_or_other']);
533 $value['select'][] = $value['other'];
535 elseif ($value['select'] == 'select_or_other') {
536 $value['select'] = $value['other'];
538 $value = $value['select'];
541 // Build a list of all valid keys expected to be submitted.
542 $options = _webform_select_options($component, TRUE);
545 if (is_array($value)) {
547 foreach ($value as $key => $option_value) {
548 // Handle options that are specified options.
549 if ($option_value !== '' && isset($options[$option_value])) {
550 // Checkboxes submit an integer value of 0 when unchecked. A checkbox
551 // with a value of '0' is valid, so we can't use empty() here.
552 if ($option_value === 0 && !$component['extra']['aslist'] && $component['extra']['multiple']) {
553 // Don't save unchecked values
556 $return[] = $option_value;
559 // Handle options that are added through the "other" field. Specifically
560 // exclude the "select_or_other" value, which is added by the select list.
561 elseif ($component['extra']['other_option'] && module_exists('select_or_other') && $option_value != 'select_or_other') {
562 $return[] = $option_value;
566 // If no elements are selected, then save an empty string to indicate that this components should not be defaulted again
571 elseif (is_string($value)) {
579 * Format the text output for this component.
581 function theme_webform_display_select($variables) {
582 $element = $variables['element'];
584 // Flatten the list of options so we can get values easily. These options
585 // may be translated by hook_webform_display_component_alter().
587 foreach ($element['#options'] as $key => $value) {
588 if (is_array($value)) {
589 foreach ($value as $subkey => $subvalue) {
590 $options[$subkey] = $subvalue;
594 $options[$key] = $value;
599 if ($element['#multiple']) {
600 foreach ((array) $element['#value'] as $option_value) {
601 if ($option_value !== '') {
602 // Administer provided values.
603 if (isset($options[$option_value])) {
604 $items[] = $element['#format'] == 'html' ? webform_filter_xss($options[$option_value]) : $options[$option_value];
606 // User-specified in the "other" field.
608 $items[] = $element['#format'] == 'html' ? check_plain($option_value) : $option_value;
614 if (isset($element['#value'][0]) && $element['#value'][0] !== '') {
615 // Administer provided values.
616 if (isset($options[$element['#value'][0]])) {
617 $items[] = $element['#format'] == 'html' ? webform_filter_xss($options[$element['#value'][0]]) : $options[$element['#value'][0]];
619 // User-specified in the "other" field.
621 $items[] = $element['#format'] == 'html' ? check_plain($element['#value'][0]) : $element['#value'][0];
626 if ($element['#format'] == 'html') {
627 $output = count($items) > 1 ? theme('item_list', array('items' => $items)) : (isset($items[0]) ? $items[0] : ' ');
630 if (count($items) > 1) {
631 foreach ($items as $key => $item) {
632 $items[$key] = ' - ' . $item;
634 $output = implode("\n", $items);
637 $output = isset($items[0]) ? $items[0] : ' ';
645 * Implements _webform_analysis_component().
647 function _webform_analysis_select($component, $sids = array(), $single = FALSE, $join = NULL) {
648 $options = _webform_select_options($component, TRUE);
650 // Create a generic query for the component.
651 $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
652 ->condition('wsd.nid', $component['nid'])
653 ->condition('wsd.cid', $component['cid'])
654 ->condition('wsd.data', '', '<>');
657 $query->condition('wsd.sid', $sids, 'IN');
661 $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
664 // Clone the query for later use, if needed.
665 if ($component['extra']['other_option']) {
666 $count_query = clone $query;
668 $other_query = clone $query;
677 // Gather the normal results first (not "other" options).
678 $query->addExpression('COUNT(wsd.data)', 'datacount');
680 ->condition('wsd.data', array_keys($options), 'IN')
681 ->fields('wsd', array('data'))
682 ->groupBy('wsd.data')
684 foreach ($result as $data) {
685 $display_option = isset($options[$data['data']]) ? $options[$data['data']] : $data['data'];
686 $rows[$data['data']] = array(webform_filter_xss($display_option), $data['datacount']);
687 $normal_count += $data['datacount'];
690 // Order the results according to the normal options array.
691 $ordered_rows = array();
692 foreach (array_intersect_key($options, $rows) as $key => $label) {
693 $ordered_rows[] = $rows[$key];
695 $rows = $ordered_rows;
699 // Add a row for displaying the total unknown or user-entered values.
700 if ($component['extra']['other_option']) {
701 $count_query->addExpression('COUNT(*)', 'datacount');
702 $full_count = $count_query->execute()->fetchField();
703 $other_count = $full_count - $normal_count;
704 $display_option = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
705 $other_text = ($other_count && !$single) ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
706 $rows[] = array($display_option, $other_text);
708 // If showing all results, execute the "other" query and append their rows.
710 $other_query->addExpression('COUNT(wsd.data)', 'datacount');
712 ->fields('wsd', array('data'))
713 ->groupBy('wsd.data');
715 $other_query->condition('wsd.data', array_keys($options), 'NOT IN');
717 $other_result = $other_query->execute();
718 foreach ($other_result as $data) {
719 $other[] = array(check_plain($data['data']), $data['datacount']);
722 array_unshift($other, '<strong>' . t('Other responses') . '</strong>');
728 'table_rows' => $rows,
729 'other_data' => $other,
734 * Implements _webform_table_component().
736 function _webform_table_select($component, $value) {
737 // Convert submitted 'safe' values to un-edited, original form.
738 $options = _webform_select_options($component, TRUE);
740 $value = (array) $value;
741 ksort($value, SORT_NUMERIC);
743 // Set the value as a single string.
744 foreach ($value as $option_value) {
745 if ($option_value !== '') {
746 if (isset($options[$option_value])) {
747 $items[] = webform_filter_xss($options[$option_value]);
750 $items[] = check_plain($option_value);
755 return implode('<br />', $items);
759 * Implements _webform_action_set_component().
761 function _webform_action_set_select($component, &$element, &$form_state, $value) {
762 // Set the value as an array for multiple select or single value otherwise.
763 if ($element['#type'] == 'checkboxes') {
764 $checkbox_values = $element['#options'];
765 array_walk($checkbox_values, function(&$value, $key) use($value) {
766 $value = (int)(strval($key) === $value);
770 $value = $component['extra']['multiple'] ? array($value) : $value;
772 $element['#value'] = $value;
773 form_set_value($element, $value, $form_state);
777 * Implements _webform_csv_headers_component().
779 function _webform_csv_headers_select($component, $export_options) {
786 if ($component['extra']['multiple'] && $export_options['select_format'] == 'separate') {
788 $headers[1][] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
789 $items = _webform_select_options($component, TRUE);
790 if ($component['extra']['other_option']) {
791 if ($export_options['header_keys']) {
792 $other_label = $component['form_key'] . '_other';
795 $other_label = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
797 $items[$other_label] = $other_label;
800 foreach ($items as $key => $item) {
801 // Empty column per sub-field in main header.
806 if ($export_options['select_keys']) {
807 $headers[2][] = $key;
810 $headers[2][] = $item;
818 $headers[2][] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
824 * Implements _webform_csv_data_component().
826 function _webform_csv_data_select($component, $export_options, $value) {
827 $options = _webform_select_options($component, TRUE);
830 if ($component['extra']['multiple']) {
831 foreach ($options as $key => $item) {
832 // Strict search is needed to avoid a key of 0 from matching an empty
834 $index = array_search((string)$key, (array)$value, TRUE);
835 if ($index !== FALSE) {
836 if ($export_options['select_format'] == 'separate') {
840 $return[] = $export_options['select_keys'] ? $key : $item;
842 unset($value[$index]);
844 elseif ($export_options['select_format'] == 'separate') {
849 // Any remaining items in the $value array will be user-added options.
850 if ($component['extra']['other_option']) {
851 $return[] = count($value) ? implode(',', $value) : '';
856 if ($export_options['select_keys']) {
860 $return = isset($options[$key]) ? $options[$key] : $key;
864 if ($component['extra']['multiple'] && $export_options['select_format'] == 'compact') {
865 $return = implode(',', (array) $return);
873 * Implements _webform_options_component().
875 * This function is confusingly an alias of _webform_select_options(). However
876 * this version is intended to be accessed publicly via
877 * webform_component_invoke(), since it is a Webform "hook", rather than an
878 * internal, private function. To get the list of select list options for
882 * $options = webform_component_invoke($component['type'], 'options', $component);
885 function _webform_options_select($component, $flat = FALSE) {
886 return _webform_select_options($component, $flat);
890 * Menu callback; Return a predefined list of select options as JSON.
892 function webform_select_options_ajax($source_name = '') {
893 $info = _webform_select_options_info();
895 $component['extra']['options_source'] = $source_name;
896 if ($source_name && isset($info[$source_name])) {
897 $options = _webform_select_options_to_text(_webform_select_options($component, FALSE, FALSE));
904 'elementId' => module_exists('options_element') ? 'edit-items-options-options-field-widget' : 'edit-extra-items',
905 'options' => $options,
908 drupal_json_output($return);
912 * Generate a list of options for a select list.
914 function _webform_select_options($component, $flat = FALSE, $filter = TRUE) {
915 if ($component['extra']['options_source']) {
916 $options = _webform_select_options_callback($component['extra']['options_source'], $component, $flat);
919 $options = _webform_select_options_from_text($component['extra']['items'], $flat);
922 // Replace tokens if needed in the options.
924 $node = node_load($component['nid']);
925 $options = _webform_select_replace_tokens($options, $node);
928 return isset($options) ? $options : array();
932 * Replace tokens in the values of a list of select options.
934 function _webform_select_replace_tokens($options, $node) {
935 foreach ($options as $key => $option) {
936 if (is_array($option)) {
937 $options[$key] = _webform_select_replace_tokens($option, $node);
940 $options[$key] = webform_replace_tokens($option, $node);
947 * Load Webform select option info from 3rd party modules.
949 function _webform_select_options_info() {
954 foreach (module_implements('webform_select_options_info') as $module) {
955 $additions = module_invoke($module, 'webform_select_options_info');
956 foreach ($additions as $key => $addition) {
957 $additions[$key]['module'] = $module;
959 $info = array_merge($info, $additions);
961 drupal_alter('webform_select_options_info', $info);
967 * Execute a select option callback.
970 * The name of the options group.
972 * The full Webform component.
974 * Whether the information returned should exclude any nested groups.
976 function _webform_select_options_callback($name, $component, $flat = FALSE) {
977 $info = _webform_select_options_info();
979 // Include any necessary files.
980 if (isset($info[$name]['file'])) {
981 $pathinfo = pathinfo($info[$name]['file']);
982 $path = ($pathinfo['dirname'] ? $pathinfo['dirname'] . '/' : '') . basename($pathinfo['basename'], '.' . $pathinfo['extension']);
983 module_load_include($pathinfo['extension'], $info[$name]['module'], $path);
986 // Execute the callback function.
987 if (isset($info[$name]['options callback']) && function_exists($info[$name]['options callback'])) {
988 $function = $info[$name]['options callback'];
990 $arguments = array();
991 if (isset($info[$name]['options arguments'])) {
992 $arguments = $info[$name]['options arguments'];
995 return $function($component, $flat, $arguments);
1000 * Utility function to split user-entered values from new-line separated
1001 * text into an array of options.
1003 * @param string $text
1004 * Text to be converted into a select option array.
1006 * Optional. If specified, return the option array and exclude any optgroups.
1008 * An array of options suitable for use as a #options property. Note that
1009 * values are not filtered and may contain tokens. Individual values should be
1010 * run through webform_replace_tokens() if displaying to an end-user.
1012 function _webform_select_options_from_text($text, $flat = FALSE) {
1013 static $option_cache = array();
1015 // Keep each processed option block in an array indexed by the MD5 hash of
1016 // the option text and the value of the $flat variable.
1019 // Check if this option block has been previously processed.
1020 if (!isset($option_cache[$flat][$md5])) {
1022 $rows = array_filter(explode("\n", trim($text)));
1024 foreach ($rows as $option) {
1025 $option = trim($option);
1027 * If the Key of the option is within < >, treat as an optgroup
1030 * creates an optgroup with the label "Group 1"
1033 * Unsets the current group, allowing items to be inserted at the root element.
1035 if (preg_match('/^\<([^>]*)\>$/', $option, $matches)) {
1036 if (empty($matches[1])) {
1040 $group = $matches[1];
1043 elseif (preg_match('/^([^|]+)\|(.*)$/', $option, $matches)) {
1045 $value = $matches[2];
1046 isset($group) ? $options[$group][$key] = $value : $options[$key] = $value;
1049 isset($group) ? $options[$group][$option] = $option : $options[$option] = $option;
1053 $option_cache[$flat][$md5] = $options;
1056 // Return our options from the option_cache array.
1057 return $option_cache[$flat][$md5];
1061 * Convert an array of options into text.
1063 function _webform_select_options_to_text($options) {
1065 $previous_key = FALSE;
1067 foreach ($options as $key => $value) {
1069 if (is_array($value)) {
1070 $output .= '<' . $key . '>' . "\n";
1071 foreach ($value as $subkey => $subvalue) {
1072 $output .= $subkey . '|' . $subvalue . "\n";
1074 $previous_key = $key;
1076 // Typical key|value pairs.
1078 // Exit out of any groups.
1079 if (isset($options[$previous_key]) && is_array($options[$previous_key])) {
1083 if ($options[$key] !== '') {
1084 $output .= $key . '|' . $value . "\n";
1086 $previous_key = $key;
1094 * Utility function to shuffle an array while preserving key-value pairs.
1096 function _webform_shuffle_options(&$array) {
1097 // First shuffle the array keys, then use them as the basis for ordering
1100 $keys = array_keys($array);
1102 foreach ($keys as $key) {
1103 $aux[$key] = $array[$key];