function hosting_site_autocomplete_sites in Hosting 7.3
Same name and namespace in other branches
- 7.4 site/hosting_site.module \hosting_site_autocomplete_sites()
Retrieve autocomplete suggestions for site names.
Parameters
$string: The typed string so far, as typed in by the user.
Return value
A list of matching sites in JSON format.
1 string reference to 'hosting_site_autocomplete_sites'
- hosting_site_menu in site/
hosting_site.module - Implements hook_menu().
File
- site/
hosting_site.module, line 133 - Contains hook implementations for Hosting site module.
Code
function hosting_site_autocomplete_sites($string) {
// Fetch the list of matching active sites. Don't use EntityFieldQuery here
// as we'd have to load the nodes afterwards to get the field data. db_query()
// is much faster as we can get away with only one DB query overall.
$query = db_select('node', 'n');
$query
->join('hosting_site', 's', 's.nid = n.nid');
$results = $query
->fields('n', array(
'nid',
'title',
))
->condition('n.type', 'site')
->condition('n.title', '%' . db_like($string) . '%', 'LIKE')
->condition('s.status', HOSTING_SITE_DELETED, '!=')
->execute();
// Save each result to the list.
$matches = array();
foreach ($results as $row) {
$matches[$row->title] = check_plain($row->title);
}
// Return the results as a JSON list.
drupal_json_output($matches);
}