You are here

function select2widget_field_widget_form in Select2 Field Widget 7.2

Same name and namespace in other branches
  1. 7 select2widget.module \select2widget_field_widget_form()

Implements hook_field_widget_form().

File

./select2widget.module, line 90

Code

function select2widget_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Abstract over the actual field columns, to allow different field types to
  // reuse those widgets.
  $value_key = key($field['columns']);
  $type = str_replace('options_', '', $instance['widget']['type']);
  $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  $required = $element['#required'];
  $has_value = isset($items[0][$value_key]);
  $settings = $instance['widget']['settings'];

  // + field_info_widget_settings($instance['widget']['type']);
  switch ($type) {
    case 'select2widget':
      $properties = _select2widget_options_properties($type, $multiple, $required, $has_value);
      $entity_type = $element['#entity_type'];
      $entity = $element['#entity'];

      // Prepare the list of options.
      $options = _select2widget_options_get_options($field, $instance, $properties, $entity_type, $entity);

      // Put current field values in shape.
      $default_value = _options_storage_to_form($items, $options, $value_key, $properties);
      $element += array(
        '#type' => 'select',
        '#default_value' => $default_value,
        // Do not display a 'multiple' select box if there is only one option.
        '#multiple' => $multiple && count($options) >= 1,
        '#options' => $options,
        '#process' => array(
          'select2widget_process_callback',
          'ajax_process_form',
        ),
        '#value_key' => $value_key,
        '#element_validate' => array(
          'select2widget_field_widget_validate',
        ),
        '#properties' => $properties,
      );
      break;
    case 'select2widgetajax':
      $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
      $entity_type = $instance['entity_type'];
      $id = 'NULL';
      if ($entity) {
        list($entity_id) = entity_extract_ids($entity_type, $entity);
        if ($entity_id) {
          $id = $entity_id;
        }
      }

      // Prepare the autocomplete path.
      $autocomplete_path = 'select2widget/ajax/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/' . $id;
      switch ($field['type']) {
        case 'taxonomy_term_reference':
          $tags = array();
          $separator = $settings['select2widgetajax']['separator'];
          foreach ($items as $item) {
            $term = taxonomy_term_load($item['tid']);
            if ($term) {
              $tags['id' . chr(9) . $term->tid] = $term->name;
            }
          }
          $tags = array_filter($tags);
          $default_tags = array_keys($tags);
          $element += array(
            '#type' => 'textfield',
            '#default_value' => implode($separator, $default_tags),
            '#autocomplete_path' => $autocomplete_path,
            '#element_validate' => array(
              'select2widget_validate_field',
            ),
            '#process' => array(
              'select2widget_taxonomy_process_callback',
              'ajax_process_form',
            ),
            '#label' => $instance['label'],
            '#settings' => $instance['widget']['settings'],
            '#title' => check_plain($instance['label']),
            '#maxlength' => 1024,
            '#init' => $tags,
          );
          break;
        case 'entityreference':
          $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
          $entity_ids = array();
          $entity_labels = array();

          // Build an array of entities ID.
          foreach ($items as $item) {
            $entity_ids[] = $item['target_id'];
          }

          // Load those entities and loop through them to extract their labels.
          $entities = entity_load($field['settings']['target_type'], $entity_ids);
          foreach ($entities as $entity_id => $entity_item) {
            $label = $handler
              ->getLabel($entity_item);
            $key = "{$label} ({$entity_id})";

            // Labels containing commas or quotes must be wrapped in quotes.
            if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
              $key = '"' . str_replace('"', '""', $key) . '"';
            }
            $entity_labels[$entity_id] = $key;
          }

          // Check original field configuration
          if (!empty($instance['field_mode']) && !empty($form_state['field'][$field['field_name']][LANGUAGE_NONE]['instance']['widget']['type']) && $form_state['field'][$field['field_name']][LANGUAGE_NONE]['instance']['widget']['type'] == 'og_complex') {
            $autocomplete_path .= '?field_mode=' . $instance['field_mode'];
          }
          $element += array(
            '#type' => 'textfield',
            '#maxlength' => 1024,
            '#default_value' => implode(',', $entity_labels),
            '#autocomplete_path' => $autocomplete_path,
            '#element_validate' => array(
              'select2widget_entity_validate_field',
            ),
            '#settings' => $instance['widget']['settings'],
            '#process' => array(
              'select2widget_entity_process_callback',
              'ajax_process_form',
            ),
            '#maxlength' => 1024,
            '#init' => $entity_labels,
          );
          break;
      }
      break;
  }
  return $element;
}