You are here

function field_reference_autocomplete_value in Field reference 7

Value callback for a field_reference autocomplete element.

1 string reference to 'field_reference_autocomplete_value'
field_reference_field_widget_form in ./field_reference.module
Implements hook_field_widget_form().

File

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

Code

function field_reference_autocomplete_value($element, $input = FALSE, $form_state) {
  if ($input === FALSE) {

    // Construct the autocomplete prefill value.
    $field_reference = $element['#default_value'];
    if (!empty($field_reference)) {
      $field_key = field_reference_key_create($field_reference);

      // Get the allowed values for this field.
      $element_field = field_info_field($element['#field_name']);
      $options = array(
        'append_id' => FALSE,
        'entity_type' => $element['#entity_type'],
        'bundle' => $element['#bundle'],
      );
      $instance = $form_state['field'][$element['#field_name']][$element['#language']]['instance'];
      $refs = field_reference_potential_references($element_field, $instance, $options);

      // Check if $field_key is a key in that list.
      if (isset($refs[$field_key])) {
        $field_label = $refs[$field_key];
      }
      else {
        $field_label = t('Missing field');
      }
      return $field_label . ' [' . $field_key . ']';
    }
  }
}