0
votes

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!

1

1 Answers

0
votes

I realized that you have to clear the Drupal Cache in order for your Hook Menu Changes to take effect.