You are here

function mvf_widget_process in Measured Value Field 6

Process an individual MVF element.

1 string reference to 'mvf_widget_process'
mvf_elements in ./mvf.module
Implementation of FAPI hook_elements().

File

./mvf.module, line 383
Measured Value Field module.

Code

function mvf_widget_process($element, $edit, $form_state, $form) {
  $field_name = $element['#field_name'];
  $field = $form['#field_info'][$field_name];
  $from_field = 'value';
  $to_field = 'value2';
  $unit_field = 'unit';
  $field_precision = isset($field['precision']) && (int) $field['precision'] > 0 ? (int) $field['precision'] : 12;
  $field_decimals = isset($field['decimals']) && (int) $field['decimals'] >= 0 ? (int) $field['decimals'] : 0;
  $field_min = isset($field['min']) ? parse_formatted_number($field['min']) : NULL;
  if (!is_numeric($field_min)) {
    $field_min = formatted_number_get_number_limit($field['type'], 'min', $field_precision, $field_decimals);
  }

  // Compute maxlength for the input textfield.
  $field_maxlength = $field_precision;
  $extra_length = 0;
  if ($field_decimals > 0) {
    $extra_length++;
  }
  if (isset($field_min) && $field_min < 0) {
    $extra_length++;
  }
  $thousands_sep = format_number_get_options('thousands_sep');
  if (!empty($thousands_sep)) {
    $extra_length += ceil($field_precision / 3);
  }
  $description = !empty($field['widget']['description']) ? t($field['widget']['description']) : '';
  if ($field['tovalue'] != 'required' && !empty($element['#default_value'][$to_field]) && $element['#default_value'][$to_field] == $element['#default_value'][$from_field]) {
    unset($element['#default_value'][$to_field]);
  }

  // Reuse formatted number element for "From" value.
  $element = formatted_number_widget_process($element, $edit, $form_state, $form);

  // Do not use title/description of the formatted number element.
  unset($element[$from_field]['#title'], $element[$from_field]['#description']);

  // Remove Format Number validation sub-element, because we don't need it.
  unset($element['_error_element']);

  // If this field uses the 'To' value, add matching element
  // for it.
  if (!empty($field['tovalue'])) {
    $element[$to_field] = $element[$from_field];

    // Format the default values.
    $default_value2 = isset($element['#value'][$to_field]) ? $element['#value'][$to_field] : '';
    if (is_numeric($default_value2)) {
      $default_value2 = format_number($default_value2, $field_decimals);
    }

    // Empty "To" value is stored as equal to the "From" value.
    // We hide it and present empty field like it's really empty.
    $element[$to_field]['#default_value'] = $element[$from_field]['#default_value'] == $default_value2 ? '' : $default_value2;

    // "To" value requirement check depends on field setting.
    $element[$to_field]['#required'] = $element['#required'] && $field['tovalue'] == 'required';

    // Adapt titles to make it clear which is the "From" and which
    // is the "To"
    $element[$from_field]['#title'] = t('From');
    $element[$to_field]['#title'] = t('To');
    $element['#fieldset_description'] = $description;
  }
  else {
    $element[$from_field]['#description'] = $description;
  }

  // Unit uses a select list element.
  $element[$unit_field] = array(
    '#type' => 'select',
    '#default_value' => isset($element['#value'][$unit_field]) ? $element['#value'][$unit_field] : array(),
    '#options' => mvf_get_widget_units($field),
    // The following values were set by the content module and need
    // to be passed down to the nested element.
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
  );
  return $element;
}