You are here

function mathfield_field_widget_form in Math Field 7

Implements hook_field_widget_form().

File

./mathfield.module, line 146
Adds a dynamic math expression field.

Code

function mathfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {

    // Do nothing to the field settings form.
    return;
  }
  $field_name = $element['#field_name'];

  // Add the form afterbuild only once.
  $afterbuild_added =& drupal_static(__FUNCTION__ . '_afterbuild', FALSE);
  if (!$afterbuild_added) {
    $form['#after_build'][] = 'mathfield_form_afterbuild';
    $afterbuild_added = TRUE;
  }

  // Check for existing value.
  if (isset($form_state['values'][$field_name][$langcode][$delta]['value'])) {
    $value = $form_state['values'][$field_name][$langcode][$delta]['value'];
  }
  elseif (isset($items[$delta]['value'])) {
    $value = $items[$delta]['value'];
  }
  else {
    $value = '';
  }

  // Substitute the decimal separator.
  $value = strtr($value, '.', $field['settings']['decimal_separator']);

  // Update the input value. Overrides user submitted data.
  $form_state['input'][$field_name][$langcode][$delta]['value'] = $value;

  // Add prefix and suffix.
  $prefix = $suffix = '';
  if (!empty($instance['settings']['prefix'])) {
    $prefixes = explode('|', $instance['settings']['prefix']);
    $prefix = field_filter_xss(array_pop($prefixes));
  }
  if (!empty($instance['settings']['suffix'])) {
    $suffixes = explode('|', $instance['settings']['suffix']);
    $suffix = field_filter_xss(array_pop($suffixes));
  }

  // Add defaults.
  $element += array(
    '#after_build' => array(
      'mathfield_widget_afterbuild',
    ),
    '#settings' => $field['settings'],
    '#widget' => $instance['widget']['type'],
  );

  // Add form validation handler only once.
  $validation_added =& drupal_static(__FUNCTION__ . '_validate', FALSE);
  if (!$validation_added) {
    $form['#validate'][] = 'mathfield_widget_validate';
    $validation_added = TRUE;
  }

  // Add ajax wrapper and settings.
  $wrapper_id = 'mathfield-' . strtr($element['#field_name'], '_', '-') . '-wrapper';
  $wrapper = array(
    '#theme_wrappers' => array(
      'container',
    ),
    '#attributes' => array(
      'id' => $wrapper_id,
    ),
  );
  $element['#ajax'] = array(
    'callback' => 'mathfield_widget_ajax',
    'wrapper' => $wrapper_id,
    'event' => 'mathfield:evaluate',
  );
  if ($instance['widget']['type'] == 'mathfield_text') {
    $element += array(
      '#type' => 'textfield',
      '#default_value' => $value,
      '#size' => $field['settings']['precision'] + 4,
      '#maxlength' => $field['settings']['precision'] + 2,
      '#number_type' => 'decimal',
      '#element_validate' => array(
        'number_field_widget_validate',
      ),
      '#field_prefix' => $prefix,
      '#field_suffix' => $suffix,
    );
    return $wrapper + array(
      'value' => $element,
    );
  }

  // Default to read-only widget. The textfield element is required for AJAX
  // functionality but is not saved.
  $element += array(
    '#type' => 'textfield',
    '#default_value' => $value,
    '#attributes' => array(
      'class' => array(
        'element-invisible',
      ),
      'readonly' => 'readonly',
    ),
    // Add the display as a field prefix.
    '#field_prefix' => $prefix . '<span class="mathfield-display">' . check_plain($value) . '</span>' . $suffix,
  );
  return $wrapper + array(
    'display' => $element,
    // The actual value is unchangeable for a read-only widget.
    'value' => array(
      '#type' => 'value',
      '#value' => $value,
    ),
  );
}