You are here

function _webform_render_select in Webform 7.4

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.3 components/select.inc \_webform_render_select()
  4. 6.2 components/select.inc \_webform_render_select()
  5. 7.3 components/select.inc \_webform_render_select()

Implements _webform_render_component().

File

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

Code

function _webform_render_select($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  $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['required'],
    '#weight' => $component['weight'],
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
    '#theme_wrappers' => array(
      'webform_element',
    ),
    '#translatable' => array(
      'title',
      'description',
      'options',
    ),
  );

  // Prevent double-wrapping of radios and checkboxes.
  if (!$component['extra']['aslist']) {
    $element['#pre_render'] = array();
  }

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

    // Make element inaccessible if there are no options as there is no point in showing it.
    $element['#access'] = FALSE;
  }
  if ($component['extra']['optrand']) {
    _webform_shuffle_options($options);
  }

  // Add HTML5 required attribute, if needed and possible (not working on more than one checkboxes).
  if ($component['required'] && ($component['extra']['aslist'] || !$component['extra']['multiple'] || count($options) == 1)) {
    $element['#attributes']['required'] = 'required';
  }

  // Add default options if using a select list with no default. This trigger's
  // Drupal 7's adding of the option for us. See @form_process_select().
  if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
    $element['#empty_value'] = '';
    if (strlen($component['extra']['empty_option'])) {
      $element['#empty_option'] = $component['extra']['empty_option'];
      $element['#translatable'][] = 'empty_option';
    }
  }

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

  // Use the component's default value if the component is currently empty.
  if (!isset($value)) {

    // The default for multiple selects is a comma-delimited list, without white-space or empty entries.
    $value = $component['extra']['multiple'] ? array_filter(array_map('trim', explode(',', $default_value)), 'strlen') : $default_value;
  }

  // Convert all values into an array; component may now be single but was previously multiple, or vice-versa.
  $value = (array) $value;

  // Set the default value. Note: "No choice" is stored as an empty string,
  // which will match a 0 key for radios; NULL is used to avoid unintentional
  // defaulting to the 0 option.
  if ($component['extra']['multiple']) {

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

    // Set the value as a single string.
    $option_value = reset($value);
    $element['#default_value'] = $option_value === '' ? NULL : $option_value;
  }
  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['#translatable'][] = 'other';
    $element['#other_title'] = $element['#title'] . ' ' . $element['#other'];
    $element['#other_title_display'] = 'invisible';
    $element['#other_unknown_defaults'] = 'other';
    $element['#other_delimiter'] = ', ';

    // Merge in Webform's #process function for Select or other.
    $element['#process'] = array_merge(element_info_property('select_or_other', '#process'), array(
      'webform_expand_select_or_other',
    ));

    // Inherit select_or_other settings or set defaults.
    $element['#disabled'] = isset($component['extra']['#disabled']) ? $component['extra']['#disabled'] : element_info_property('select_or_other', 'disabled');
    $element['#size'] = isset($component['extra']['#size']) ? $component['extra']['#size'] : element_info_property('select_or_other', 'size');
    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';
      $element['#theme_wrappers'] = array_merge(array(
        'checkboxes',
      ), $element['#theme_wrappers']);
      $element['#process'] = array_merge(element_info_property('checkboxes', '#process'), array(
        'webform_expand_select_ids',
      ));
    }
    else {

      // Set display as a radio set.
      $element['#type'] = 'radios';
      $element['#theme_wrappers'] = array_merge(array(
        'radios',
      ), $element['#theme_wrappers']);
      $element['#process'] = array_merge(element_info_property('radios', '#process'), array(
        'webform_expand_select_ids',
      ));
    }
  }
  return $element;
}