You are here

function _webform_render_select in Webform 6.3

Same name and namespace in other branches
  1. 5.2 components/select.inc \_webform_render_select()
  2. 5 components/select.inc \_webform_render_select()
  3. 6.2 components/select.inc \_webform_render_select()
  4. 7.4 components/select.inc \_webform_render_select()
  5. 7.3 components/select.inc \_webform_render_select()

Implements _webform_render_component().

File

components/select.inc, line 284
Webform module multiple select component.

Code

function _webform_render_select($component, $value = NULL, $filter = TRUE) {
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  $element = array(
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
    '#theme_wrappers' => array(
      'webform_element_wrapper',
    ),
    '#pre_render' => array(
      'webform_element_title_display',
    ),
    '#post_render' => array(
      'webform_element_wrapper',
    ),
    '#translatable' => array(
      'title',
      'description',
      'options',
    ),
  );

  // Convert the user-entered options list into an array.
  $default_value = $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'];
  $options = _webform_select_options($component, !$component['extra']['aslist'], $filter);
  if ($component['extra']['optrand']) {
    _webform_shuffle_options($options);
  }

  // Add default options if using a select list with no default. This matches
  // Drupal 7's handling of select lists.
  if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
    $options = array(
      '' => $component['mandatory'] ? t('- Select -') : t('- None -'),
    ) + $options;
  }

  // Set the component options.
  $element['#options'] = $options;

  // Set the default value.
  if (isset($value)) {
    if ($component['extra']['multiple']) {

      // Set the value as an array.
      $element['#default_value'] = array();
      foreach ((array) $value as $key => $option_value) {
        $element['#default_value'][] = $option_value;
      }
    }
    else {

      // Set the value as a single string.
      $element['#default_value'] = '';
      foreach ((array) $value as $option_value) {
        $element['#default_value'] = $option_value;
      }
    }
  }
  elseif ($default_value !== '') {

    // Convert default value to a list if necessary.
    if ($component['extra']['multiple']) {
      $varray = explode(',', $default_value);
      foreach ($varray as $key => $v) {
        $v = trim($v);
        if ($v !== '') {
          $element['#default_value'][] = $v;
        }
      }
    }
    else {
      $element['#default_value'] = $default_value;
    }
  }
  elseif ($component['extra']['multiple']) {
    $element['#default_value'] = array();
  }
  if ($component['extra']['other_option'] && module_exists('select_or_other')) {

    // Set display as a select_or_other element:
    $element['#type'] = 'select_or_other';
    $element['#other'] = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
    $element['#other_unknown_defaults'] = 'other';
    $element['#other_delimiter'] = ', ';
    $element['#process'] = array(
      'select_or_other_process',
      'webform_expand_select_or_other',
    );
    if ($component['extra']['multiple']) {
      $element['#multiple'] = TRUE;
      $element['#select_type'] = 'checkboxes';
    }
    else {
      $element['#multiple'] = FALSE;
      $element['#select_type'] = 'radios';
    }
    if ($component['extra']['aslist']) {
      $element['#select_type'] = 'select';
    }
  }
  elseif ($component['extra']['aslist']) {

    // Set display as a select list:
    $element['#type'] = 'select';
    if ($component['extra']['multiple']) {
      $element['#size'] = 4;
      $element['#multiple'] = TRUE;
    }
  }
  else {
    if ($component['extra']['multiple']) {

      // Set display as a checkbox set.
      $element['#type'] = 'checkboxes';

      // Drupal 6 hack to properly render on multipage forms.
      $element['#process'] = array(
        'webform_expand_checkboxes',
      );
      $element['#pre_render'][] = 'webform_expand_select_ids';
    }
    else {

      // Set display as a radio set.
      $element['#type'] = 'radios';
      $element['#pre_render'][] = 'webform_expand_select_ids';
    }
  }
  return $element;
}