0

I've an entity called "guy" with a field "location" and i've 9-10.000 location that i want to reference. I must use a simple list (1|bla, 2|blabla) or an entity reference field that target the location entity? I need the autocomplete function but drupal is very slow with a great number of location. But i don't know if i can load a list of 9-10k lines. I don't wont to use taxonomy. To speed up the autocomplete, how i can create a list of frequently selected location?

1 Answer 1

1

I dont think there is a module you can use.

The form will contain an element such as

<?php
  $form['city'] = array(
    '#type' => 'textfield',
    '#title' => 'City',
    '#maxlength' => 128,
    '#autocomplete_path' => 'cities/autocomplete',
  );
?>

Addition to hook_menu

You must add #autocomplete_path to your hook_menu function.

<?php
    function cities_menu(){
    ...
    // path with autocomplete function for cities
    $items['cities/autocomplete'] = array(
      'title' => 'Autocomplete for cities',
      'page callback' => '_cities_autocomplete',
      'access arguments' => array('use autocomplete'),  //or whatever permission makes sense
      'type' => MENU_CALLBACK
    );
    return $items;
?>

Make a call to the Database.

<?php
/**
 * autocomplete helper
 * $string = string for search
 */
function _cities_autocomplete($string) {
  // Fetch most used cities. 
  $matches = array();
  $result = db_select('cities', 'c')
    ->fields('c', array('city'))
    ->condition('city', '%' . db_like($string) . '%', 'LIKE')
    ->execute();

  // save the query to matches
  foreach ($result as $row) {
    $matches[$row->city] = check_plain($row->city);
  }

  // Return the result to the form in json
  drupal_json_output($matches);
}
?>

Make sure your autocomplete handler has appropriate menu permissions set on it,

From : Textfield that uses autocomplete

2
  • Mumble... i'm very newbie and don't think i can implement this. I don't know hot to create a data base table with the data. I don't want to create a custom module but just know the simpler and faster way to use the existing module for selecting one item in a list of thousand.
    – user11743
    Dec 7, 2012 at 7:52
  • @user11743 as i said i am unfamiliar with any modules act such way.
    – niksmac
    Dec 7, 2012 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.