You are here

function field_reference_autocomplete in Field reference 7

Menu callback for the autocomplete results.

4 string references to 'field_reference_autocomplete'
field_reference_field_widget_form in ./field_reference.module
Implements hook_field_widget_form().
field_reference_field_widget_form_alter in ./field_reference.module
Implements hook_field_widget_form_alter().
field_reference_field_widget_settings_form in ./field_reference.module
Implements hook_field_widget_settings_form().
field_reference_menu in ./field_reference.module
Implements hook_menu().

File

./field_reference.module, line 937
Defines a field type for referencing a field from another.

Code

function field_reference_autocomplete($entity_type, $bundle, $field_name, $string = '') {

  // If the request has a '/' in the search text, then the menu system will have
  // split it into multiple arguments, recover the intended $string.
  $args = func_get_args();

  // Shift off the $entity_type argument.
  array_shift($args);

  // Shift off the $bundle argument.
  array_shift($args);

  // Shift off the $field_name argument.
  array_shift($args);
  $string = implode('/', $args);
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  $options = array(
    'string' => $string,
    'match' => $instance['widget']['settings']['autocomplete_match'],
    'limit' => 10,
  );
  $references = field_reference_potential_references($field, $instance, $options);
  $matches = array();
  foreach ($references as $id => $row) {

    // If the user is not appending id's, we'll do it anyway for autocomplete purposes.
    $appendage = !$field['settings']['append_id'] ? ' [' . $id . ']' : NULL;
    $matches[$row . $appendage] = '<div class="reference-autocomplete">' . filter_xss($row) . '</div>';
  }
  drupal_json_output($matches);
}