materio-base-legacy/materiobasemod.pages.inc
bachy ad7809415c first import :: search autocomplete form
Signed-off-by: bachy <git@g-u-i.net>
2012-07-19 18:31:59 +02:00

54 lines
1.5 KiB
PHP

<?php
/**
* materiobase_search_autocomplete()
*
* inspired by taxonomy_autocomplete()
*
*/
function materiobase_search_autocomplete($typed = ''){
// If the request has a '/' in the search text, then the menu system will have
// split it into multiple arguments, recover the intended $tags_typed.
$args = func_get_args();
$typed = implode('/', $args);
/*
TODO riche serach engine + \\ etc gmail like
*/
if ($typed != '') {
// Part of the criteria for the query come from the field's own settings.
$vids = array();
$vocabularies = taxonomy_vocabulary_get_names();
foreach ($vocabularies as $voc) {
$vids[] = $voc->vid;
}
$query = db_select('taxonomy_term_data', 't');
$query->addTag('translatable');
$query->addTag('term_access');
// Select rows that match by term name.
$tags_return = $query
->fields('t', array('tid', 'name'))
->condition('t.vid', $vids)
->condition('t.name', '%' . db_like($typed) . '%', 'LIKE')
->range(0, 10)
->execute()
->fetchAllKeyed();
$term_matches = array();
foreach ($tags_return as $tid => $name) {
$n = $name;
// Term names containing commas or quotes must be wrapped in quotes.
// if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
// $n = '"' . str_replace('"', '""', $name) . '"';
// }
$term_matches[$n] = check_plain($name);
}
}
drupal_json_output($term_matches);
}