You are here

function _webform_render_number in Webform 6.3

Same name and namespace in other branches
  1. 7.4 components/number.inc \_webform_render_number()
  2. 7.3 components/number.inc \_webform_render_number()

Implements _webform_render_component().

File

components/number.inc, line 269
Webform module number component.

Code

function _webform_render_number($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',
    '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
    '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
    '#attributes' => $component['extra']['attributes'],
    '#element_validate' => array(
      '_webform_validate_number',
    ),
    '#theme_wrappers' => array(
      'webform_element_wrapper',
    ),
    '#pre_render' => array(
      'webform_element_title_display',
    ),
    '#post_render' => array(
      'webform_element_wrapper',
    ),
    '#min' => $component['extra']['min'],
    '#max' => $component['extra']['max'],
    '#step' => $component['extra']['step'] ? abs($component['extra']['step']) : '',
    '#integer' => $component['extra']['integer'],
    '#point' => $component['extra']['point'],
    '#separator' => $component['extra']['separator'],
    '#decimals' => $component['extra']['decimals'],
    '#translatable' => array(
      'title',
      'description',
      'field_prefix',
      'field_suffix',
    ),
  );

  // Set the decimal count to zero for integers.
  if ($element['#integer'] && $element['#decimals'] === '') {
    $element['#decimals'] = 0;
  }

  // Flip the min and max properties to make min less than max if needed.
  if ($element['#min'] !== '' && $element['#max'] !== '' && $element['#min'] > $element['#max']) {
    $max = $element['#min'];
    $element['#min'] = $element['#max'];
    $element['#max'] = $max;
  }

  // Ensure #step starts with a zero if a decimal.
  if (!is_int($element['#step'] * 1)) {
    $decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
    $element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
  }
  if ($component['extra']['type'] == 'textfield') {

    // Render as textfield.
    $element['#type'] = 'webform_number';

    // Set the size property based on #max, to ensure consistent behavior for
    // browsers that do not support type = number.
    if ($element['#max']) {
      $element['#size'] = strlen($element['#max']) + 1;
    }
  }
  else {

    // Render as select.
    $element['#type'] = 'select';

    // Create user-specified options list as an array.
    $element['#options'] = _webform_number_select_options($component);

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

  // Set user-entered values.
  if (isset($value[0])) {

    // If the value has been standardized, convert it to the expected format
    // for display to the user.
    if (webform_number_format_match($value[0], '.', '')) {
      $element['#default_value'] = _webform_number_format($component, $value[0]);
    }
    else {
      $element['#default_value'] = $value[0];
    }
  }

  // Enforce uniqueness.
  if ($component['extra']['unique']) {
    $element['#element_validate'][] = 'webform_validate_unique';
  }

  // Set readonly if disabled.
  if ($component['extra']['disabled']) {
    if ($filter) {
      $element['#attributes']['readonly'] = 'readonly';
    }
    else {
      $element['#disabled'] = TRUE;
    }
  }
  return $element;
}