You are here

function number_process in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6 modules/number/number.module \number_process()
  2. 6.2 modules/number/number.module \number_process()

Process an individual element.

Build the form element. When creating a form using FAPI #process, note that $element['#value'] is already set.

The $fields array is in $form['#field_info'][$element['#field_name']].

1 string reference to 'number_process'
number_elements in modules/number/number.module
Implementation of FAPI hook_elements().

File

modules/number/number.module, line 440
Defines numeric field types.

Code

function number_process($element, $edit, $form_state, $form) {
  $field_name = $element['#field_name'];
  $field = $form['#field_info'][$field_name];
  $field_key = $element['#columns'][0];
  $value = isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : '';
  $value = isset($field['decimal']) ? str_replace('.', $field['decimal'], $value) : $value;
  $element[$field_key] = array(
    '#type' => 'textfield',
    '#default_value' => $value,
    // Need to allow a slightly larger size that the field length to allow
    // for some configurations where all characters won't fit in input field.
    '#size' => isset($field['precision']) ? $field['precision'] + 2 : 12,
    '#maxlength' => isset($field['precision']) ? $field['precision'] : 10,
    '#attributes' => array(
      'class' => 'number',
    ),
    // The following values were set by the content module and need
    // to be passed down to the nested element.
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
  );
  $prefixes = array();
  $suffixes = array();

  // Make sure we don't wipe out element validation added elsewhere.
  if (empty($element['#element_validate'])) {
    $element['#element_validate'] = array();
  }
  if (!empty($field['prefix'])) {
    $prefixes = explode('|', $field['prefix']);
    $element[$field_key]['#field_prefix'] = content_filter_xss(array_pop($prefixes));
  }
  if (!empty($field['suffix'])) {
    $suffixes = explode('|', $field['suffix']);
    $element[$field_key]['#field_suffix'] = content_filter_xss(array_pop($suffixes));
  }
  switch ($field['type']) {
    case 'number_float':
      $element['#element_validate'][] = 'number_float_validate';
      break;
    case 'number_integer':
      $element['#element_validate'][] = 'number_integer_validate';
      break;
    case 'number_decimal':
      $element['#element_validate'][] = 'number_decimal_validate';
      $element['#decimal'] = isset($field['decimal']) ? $field['decimal'] : '.';
      $element['#precision'] = isset($field['precision']) ? $field['precision'] : 10;
      $element['#scale'] = isset($field['scale']) ? $field['scale'] : 2;
      break;
  }

  // Used so that hook_field('validate') knows where to flag an error.
  $element['_error_element'] = array(
    '#type' => 'value',
    '#value' => implode('][', array_merge($element['#parents'], array(
      $field_key,
    ))),
  );
  return $element;
}