You are here

function user_reference_autocomplete_value in References 7.2

Value callback for a user_reference autocomplete element.

Substitute in the user name for the uid.

@codingStandardsIgnoreStart

1 string reference to 'user_reference_autocomplete_value'
user_reference_field_widget_form in user_reference/user_reference.module
Implements hook_field_widget_form().

File

user_reference/user_reference.module, line 687
Defines a field type for referencing a user from a node.

Code

function user_reference_autocomplete_value($element, $input = FALSE, $form_state) {

  // @codingStandardsIgnoreEnd
  if ($input === FALSE) {

    // We're building the displayed 'default value': expand the raw uid into
    // "user name [uid:n]".
    $uid = $element['#default_value'];
    if (!empty($uid)) {
      $q = db_select('users', 'u');
      $q
        ->addField('u', 'name');
      $q
        ->condition('u.uid', $uid)
        ->range(0, 1);
      $result = $q
        ->execute();

      // @todo If no result (user doesn't exist).
      $value = $result
        ->fetchField();
      $value .= ' [uid:' . $uid . ']';
      return $value;
    }
  }
}