[support] autocomplete module code review

Kevin Davison kevin at quevin.com
Tue Aug 10 23:21:31 UTC 2010


Do you see much wrong with this autocomplete module code (form_alter on Webform fields)? It works fine, but I think it can be cleaned up:

function college_autocomplete_menu() {
  $items["as/college/autocomplete"] = array(
    'title' => 'Autocomplete for agencies',
    'page callback' => 'college_autocomplete_callback',
    'page arguments' => array(3), // Note this is 3 based on the path ("my_special/path...") set above
    'access callback' =>TRUE,
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
   );
  return $items;
}

function college_autocomplete_form_alter(&$form, $form_state, $form_id) {
  if($form_id == "webform_client_form_108" ){
// A better way to consolidate these fields into one... array?
    $form['submitted']['college_1']['college_choice_1']['#autocomplete_path'] = "as/college/autocomplete";
    $form['submitted']['college_2']['college_choice_2']['#autocomplete_path'] = "as/college/autocomplete";
    $form['submitted']['college_3']['college_choice_3']['#autocomplete_path'] = "as/college/autocomplete";
    $form['submitted']['college_4']['college_choice_4']['#autocomplete_path'] = "as/college/autocomplete";    
    $form['submitted']['college_5']['college_choice_5']['#autocomplete_path'] = "as/college/autocomplete";
    $form['submitted']['college_6']['college_choice_6']['#autocomplete_path'] = "as/college/autocomplete";
    $form['submitted']['college_7']['college_choice_7']['#autocomplete_path'] = "as/college/autocomplete";
    $form['submitted']['college_8']['college_choice_8']['#autocomplete_path'] = "as/college/autocomplete";
  } 
}

// A callback funtion to send back suggestion based on the bit of a string it receives.
function college_autocomplete_callback($string = '') {
  $matches = array();
// wrap my query with anything more for security?
  $result = db_query_range("SELECT name FROM {term_data} WHERE LOWER(name) LIKE LOWER('%%%s%%')", $string, 0, 5);
  while ($row=db_fetch_object($result)) {
    $matches[$row->name] = check_plain($row->name);
  }
  print drupal_json($matches);
  exit;
}

Thanks!

Kevin

--- 

Quevin, LLC
 Quevin.com
 twitter.com/Quevin
 linkedin.com/in/quevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20100810/34f8fc2c/attachment.html 


More information about the support mailing list