You are here

function _webform_render_select in Webform 5

Same name and namespace in other branches
  1. 5.2 components/select.inc \_webform_render_select()
  2. 6.3 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()

Build a form item array containing all the properties of this component.

Parameters

$component: An array of information describing the component, directly correlating to the webform_component database schema.

Return value

An array of a form item to be displayed on the client-side webform.

1 call to _webform_render_select()
_webform_submission_display_select in components/select.inc
Display the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions".

File

components/select.inc, line 71

Code

function _webform_render_select($component) {
  $form_item = array(
    '#title' => htmlspecialchars($component['name'], ENT_QUOTES),
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#description' => _webform_filtervalues($component['extra']['description']),
    '#prefix' => '<div class="webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">',
    '#suffix' => '</div>',
  );

  // Convert the user-entered options list into an array.
  $default_value = _webform_filtervalues($component['value'], FALSE);
  $rows = explode("\n", _webform_filtervalues($component['extra']['items'], FALSE));
  if ($component['extra']['aslist'] == 'Y' && $component['extra']['multiple'] != 'Y') {
    $options = array(
      '' => t('select...'),
    );
  }
  else {
    $options = array();
  }
  foreach ($rows as $row) {
    $row = trim($row);
    if (preg_match('/^([^"|]+)\\|(.*)$/', $row, $matches)) {
      $options[$matches[1]] = $matches[2];
    }
    else {
      $options[_webform_safe_name($row)] = $row;
    }
  }

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

  // Set the default value.
  if ($default_value) {

    // Convert default value to a list if necessary.
    if ($component['extra']['multiple'] == 'Y') {
      if (strpos($default_value, ',')) {
        $varray = explode(',', $default_value);
        foreach ($varray as $key => $v) {
          if (array_key_exists(_webform_safe_name($v), $options)) {
            $form_item['#default_value'][] = _webform_safe_name($v);
          }
          else {
            $form_item['#default_value'][] = $v;
          }
        }
      }
      else {
        if (array_key_exists(_webform_safe_name($default_value), $options)) {
          $form_item['#default_value'] = array(
            _webform_safe_name($default_value),
          );
        }
        else {
          $form_item['#default_value'] = array(
            $default_value,
          );
        }
      }
    }
    else {
      if (array_key_exists(_webform_safe_name($default_value), $options)) {
        $form_item['#default_value'] = _webform_safe_name($default_value);
      }
      else {
        $form_item['#default_value'] = $default_value;
      }
    }
  }
  if ($component['extra']['aslist'] == 'Y') {

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

      // Set display as a checkbox set.
      $form_item['#type'] = 'checkboxes';
    }
    else {

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