To add auto_complete to the from field, you need to do the following:
1st: use hook_for_alter () to add the auto_complete path to the text field
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "YOUR-CONTENT-TYPE_node_form")
{
$form['field_YOUR-FIELD']['und'][0]['value']['#autocomplete_path'] = 'my-module/autocomplete/path';
}
}
2nd: hook_menu(), , , #autocomplete_path
function YOUR_MODULE_menu()
{
$items = array();
$items['my-module/autocomplete/path'] = array(
'page callback' => 'your_module_autocomplete_callback',
'access callback' => TRUE,
'weight' => 1,
'type' => MENU_CALLBACK,
);
return $items;
}
3rd last: ,
function your_module_autocomplete_callback($string)
{
$items = array();
$query = db_select('node', 'n');
$value = $query->fields('n', array('title'));
$value = $query->condition(db_and()->condition('n.type', 'YOUR_CONTENT_TYPE')->condition('title', '%' . db_like($string) . '%', 'LIKE'))->orderRandom()->execute();
$i = 0;
foreach ($value as $val)
{
$items[$val->name] = check_plain($val->name);
}
print drupal_json_output($items);
exit();
}
, ... .