Autocomplete

function mymodule_myform() { $form['myfield'] = array( '#type' => 'textfield', '#title' => 'My field', '#autocomplete_path' => 'mymodule/autocomplete', // <-- ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', ); return $form; }
function mymodule_menu() { $items['mymodule/autocomplete'] = array( 'page callback' => 'mymodule_autocomplete', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; }
function mymodule_autocomplete($string) { // для примера, возвратим заголовки нод $result = db_select('node', 'n') ->fields('n', array('title')) ->condition('title', '%' . db_like($string) . '%', 'LIKE') ->range(0, 10) ->execute(); $matches = array(); foreach ($result as $row) { $matches[$row->title] = check_plain($row->title); } drupal_json_output($matches); }