You are here

function autocomplete_deluxe_field_widget_form in Autocomplete Deluxe 7

Same name and namespace in other branches
  1. 7.2 autocomplete_deluxe.module \autocomplete_deluxe_field_widget_form()

Implements hook_field_widget_form().

File

./autocomplete_deluxe.module, line 172
Define enhanced autocomplete wdiget.

Code

function autocomplete_deluxe_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $type = str_replace('autocomplete_deluxe_', '', $instance['widget']['type']);
  $element += array(
    '#type' => 'autocomplete_deluxe',
    '#size' => $instance['widget']['settings']['size'],
  );
  $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? TRUE : FALSE;
  switch ($type) {
    case 'taxonomy':
      $tags = array();
      foreach ($items as $item) {
        $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
      }
      $element['#element_validate'] = array(
        'taxonomy_autocomplete_validate',
      );
      if (isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms']) && $instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'deny') {
        $options = autocomplete_deluxe_allowed_terms($field);
        $default_values = array();
        foreach ($tags as $tag) {
          $default_values[$tag->name] = $tag->name;
        }
        $element += array(
          '#autocomplete_options' => $options,
          '#multiple' => $multiple && count($options) > 1,
          '#default_value' => $default_values,
        );
      }
      else {
        $element += array(
          '#multiple' => $multiple,
          '#autocomplete_deluxe_path' => url($instance['widget']['settings']['autocomplete_deluxe_path'] . '/' . $field['field_name'], array(
            'absolute' => TRUE,
          )),
          '#default_value' => autocomplete_deluxe_taxonomy_implode_tags($tags),
        );
      }
      $form_state['autocomplete_deluxe']['terms'] = TRUE;
      break;
    case 'list':
      $value_key = key($field['columns']);
      $type = str_replace('options_', '', $instance['widget']['type']);
      $required = $element['#required'];
      $has_value = isset($items[0][$value_key]);
      $properties = _options_properties($type, $multiple, $required, $has_value);

      // Prepare the list of options.
      $options = _options_get_options($field, $instance, $properties);
      $default_value = _options_storage_to_form($items, $options, $value_key, $properties);
      $element += array(
        '#autocomplete_options' => $options,
        '#autocomplete_min_length' => 0,
        '#multiple' => $multiple && count($options) > 1,
        '#value_key' => $value_key,
        '#element_validate' => array(
          'options_field_widget_validate',
        ),
        '#properties' => $properties,
        '#default_value' => $default_value,
      );
      break;
  }
  return $element;
}