drupal adding a user reference field to a template

596 views Asked by At

I have a template file that I want to print a the cck user reference form field on.

Is this possible? Note, this is not the field value, but the field form. Also asking for just the field, not creating a form first and then using drupal_get_form()

Thanks!

Edit: If this is not possible that's fine too, I just would like to know

Edit2: i just need the autocomplete mechanism so I can grab the uid in js from searching for the username

1

There are 1 answers

1
Alex Petrov On BEST ANSWER

If you just need autocomplete mechanism I would suject you to use jQuery autocomplete plugin - http://docs.jquery.com/Plugins/autocomplete.

you can just output in the template something like that:

print '<input type="text" id="user-autocomplete">';

Then in javascript code

$(document).ready('function(){
  $('#user-autocomplete').autocomplete(some-ajax-url-here)
}');

you also will need to create an ajax callback page somewhere in your module:

function YOUR_MODULE_NAME_menu(){
   $items = array();
   $items['user-autocomplete-ajax-page'] = array(
     'title' => 'AJAX:get user',
     'page callback' => 'get_user'
   );
}
function get_user(){
  $sql = 'SELECT uid, name FROM {users} WHERE name LIKE ("%s")';
  $result = db_query($sql,$_GET['request']);
  $res_str = '';
  while($object = db_fetch_object($result)){
    $res_str .= $object->name.' ['.$object->uid."]\n";
  }
  print $res_str;
}

I didn't test the code, but I guess it should work, may be with some minor changes.