You are here

function autocomplete_widgets_field_widget_form in Autocomplete Widgets for Text and Number Fields 7

Implementation of hook_field_widget_form().

File

./autocomplete_widgets.module, line 122
Provides autocomplete widgets for Text and Number fields.

Code

function autocomplete_widgets_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element['value'] = array(
    '#columns' => $element['#columns'],
    '#field_name' => $element['#field_name'],
    '#field_parents' => $element['#field_parents'],
    '#language' => $element['#language'],
    '#description' => $element['#description'],
    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
    '#required' => $element['#required'],
    '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
    '#delta' => $delta,
    '#title' => $element['#title'],
    '#size' => $instance['widget']['settings']['size'],
  );
  if (isset($field['settings']['max_length'])) {
    $element['value']['#maxlength'] = $field['settings']['max_length'];
  }
  switch ($instance['widget']['type']) {
    case 'autocomplete_widgets_flddata':
    case 'autocomplete_widgets_suggested':
    case 'autocomplete_widgets_node_reference':
      $element['value'] = array_merge($element['value'], array(
        '#type' => 'textfield',
        '#autocomplete_path' => 'autocomplete_widgets/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/' . $element['#field_name'],
      ));
      break;
    case 'autocomplete_widgets_allowvals':

      // Get the label for key in hidden text field.
      $keys = array(
        $element['value']['#default_value'],
      );
      module_load_include('inc', 'autocomplete_widgets', 'autocomplete_widgets.common');
      $options = $keys[0] != '' ? _autocomplete_widgets_get_options($instance, '', '', $keys, 1) : NULL;
      $element['value'] = array_merge($element['value'], array(
        '#type' => 'textfield',
        '#process' => array(
          'autocomplete_widgets_element_process',
          'form_process_autocomplete',
        ),
        '#title' => $element['#title'],
        '#field_name' => $element['#field_name'],
        '#field_parents' => $element['#field_parents'],
        '#description' => $element['#description'],
        '#columns' => $element['#columns'],
        '#autocomplete_path' => 'autocomplete_widgets/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/' . $element['#field_name'],
        '#language' => $element['#language'],
        '#default_value' => isset($options) ? current($options) : '',
        '#required' => $element['#required'],
        '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
        '#delta' => $delta,
        '#file' => drupal_get_path('module', 'autocomplete_widgets') . '/autocomplete_widgets.common.inc',
        '#element_validate' => array(
          '_autocomplete_widgets_validate_allowvals',
        ),
      ));
      break;
  }
  return $element;
}