You are here

function _webform_render_number in Webform 7.4

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

Implements _webform_render_component().

File

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

Code

function _webform_render_number($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',
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
    '#required' => $component['required'],
    '#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',
    ),
    '#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',
      'placeholder',
    ),
  );
  if ($component['required']) {
    $element['#attributes']['required'] = 'required';
  }
  if ($component['extra']['placeholder']) {
    $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
  }

  // 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 (filter_var((double) $element['#step'], FILTER_VALIDATE_INT) === FALSE) {
    $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 trigger's
    // Drupal 7's adding of the option for us. See form_process_select().
    if ($component['extra']['type'] == 'select' && $element['#default_value'] === '') {
      $element['#empty_value'] = '';
    }
  }

  // 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;
}