简体   繁体   中英

Drupal 7 Form Textfield Autocomplete Not Working

I am new to Drupal and am trying to create a form with a dynamic text field that contains an Autocomplete Feature. This Autocomplete feature will fetch values from a database.

I have the following code in place:

Form:

function site_finder_form($form, &$form_state) {

    $form['site_finder_company_name'] = array(
        '#type' => 'textfield',        
         '#autocomplete_path' => 'companies/autocomplete',
    );

    /* Additional Form Fields here */

    return $form;
}

Hook Menu:

function site_finder_menu()
{

    // path with autocomplete function for companies
    $items['companies/autocomplete'] = array(
        'page callback' => '_site_finder_autocomplete',
        'access arguments' => array('access companies autocomplete'),
        'type' => MENU_CALLBACK
    );
    return $items;
}

Autocomplete Function:

function _site_finder_autocomplete($string) {
    $matches = array();

    // Select Rows that match the query

    $companies =       db_select('company_info', 'e')
                     ->fields('e', array('Name'))
                     ->condition('Name', '%' . db_like($string) . '%', 'LIKE')
                     ->execute();

    // Query DB to get matches

    foreach ($companies as $company){
        $matches[$company->Name] = check_plain($company->Name);
    }

    drupal_json_output($matches);

}

I've gone through a couple of tutorials to see how the autocomplete feature works in Drupal and I've followed the proper naming conventions for the Hook Menu and Autocomplete functions, so I'm not quite sure why this isn't working when I type a value into the textfield.

Any help is greatly appreciated!

我意识到您必须清除Drupal缓存,才能使“挂钩菜单”更改生效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM