You are here

function apachesolr_reference_field_widget_form in Apachesolr Reference 7

Implements hook_field_widget_form().

File

./apachesolr_reference.module, line 419
functionality for creating reference fields to apache solr objects.

Code

function apachesolr_reference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $entity_type = $instance['entity_type'];
  $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
  $field_settings = $field['settings'];
  if ($instance['widget']['type'] == 'apachesolr_reference_autocomplete') {

    // Prepare the autocomplete path.
    if (!empty($instance['widget']['settings']['path'])) {
      $autocomplete_path = $instance['widget']['settings']['path'];
    }
    else {
      $autocomplete_path = 'apachesolr-reference/autocomplete';
    }
    $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';

    // Use <NULL> as a placeholder in the URL when we don't have an entity.
    // Most webservers collapse two consecutive slashes.
    $id = 'NULL';
    if ($entity) {
      list($eid) = entity_extract_ids($entity_type, $entity);
      if ($eid) {
        $id = $eid;
      }
      $entity_language = _apachesolr_reference_entity_language($entity_type, $entity);

      // Add entity language if not 'und'.
      if ($entity_language != LANGUAGE_NONE) {
        $field_settings['language'] = $entity_language;
      }
    }

    // Add entity ID and language.
    $autocomplete_path .= $id . '/' . $entity_language;
    $items = _apachesolr_reference_fetch_field_items($items, $field_settings);
    if (isset($items[$delta])) {
      $previous_val = $items[$delta];
      $previous_val = $previous_val->{$field}['settings']['search_fields']['label'] . '(' . $previous_val->{$field}['settings']['search_fields']['id'] . ')';
    }
    else {
      $previous_val = '';
    }

    // Build auto complete field.
    $element += array(
      '#type' => 'textfield',
      '#maxlength' => 1024,
      '#autocomplete_path' => $autocomplete_path,
      '#element_validate' => array(
        '_apachesolr_reference_autocomplete_validate',
      ),
      '#default_value' => $previous_val,
    );
    return array(
      'target_id' => $element,
    );
  }
}