Skip to content

Instantly share code, notes, and snippets.

@sinsunsan
Created November 12, 2012 15:38
Show Gist options
  • Select an option

  • Save sinsunsan/4060035 to your computer and use it in GitHub Desktop.

Select an option

Save sinsunsan/4060035 to your computer and use it in GitHub Desktop.
Use a view in a autocomplete callback
/**
* Implement hook_autocomplete_alter
*
*/
function common_nodequeue_autocomplete() {
module_load_include('inc', 'nodequeue', 'includes/nodequeue.admin');
$args = func_get_args();
$test = array_shift($args);
if ($test != 'autocomplete') return drupal_json_output(array());
$sqid = array_shift($args);
$string = strtolower(implode('/', $args));
st_nodes_by_date
$view = views_get_view('list_nodes_by_date');
$view->set_arguments(array($sqid));
$view->init_display(); $view->pre_execute(); $view->execute();
$nodes_in_queue = $view->result;
foreach ($nodes_in_queue as $key => $row) {
$niq[$row->nid] = TRUE;
}
unset($nodes_in_queue);
$view = views_get_view('list_nodes_by_date');
$view->set_arguments(array('all'));
$view->init_display(); $view->pre_execute(); $view->execute();
$nodes = $view->result;
$return = array();
$limit = 10;
foreach ($nodes as $row) {
if (isset($niq[$row->nid])) continue;
$accents = array('à','á','â','ã','ä', 'ç','è','é','ê','ë', 'ì','í','î','ï', 'ñ', 'ò','ó','ô','õ','ö', 'ù','ú','û','ü', 'ý','ÿ', 'À','Á','Â','Ã','Ä', 'Ç','È','É','Ê','Ë', 'Ì','Í','Î','Ï', 'Ñ', 'Ò','Ó','Ô','Õ','Ö', 'Ù','Ú','Û','Ü', 'Ý');
$normal = array('a','a','a','a','a', 'c', 'e','e','e','e', 'i','i','i','i', 'n', 'o','o','o','o','o', 'u','u','u','u', 'y','y', 'A','A','A','A','A', 'C', 'E','E','E','E', 'I','I','I','I', 'N', 'O','O','O','O','O', 'U','U','U','U','Y');
$search = check_plain(str_replace($accents, $normal, trim($row->node_title))) . ' [nid: ' . (integer)$row->nid . ']';
$val = check_plain(trim($row->node_title)) . ' [nid: ' . (integer)$row->nid . ']';
if (stripos($search, $string) !== FALSE && $limit > 0) {
$limit--;
$return[$val] = $val;
}
}
drupal_json_output(drupal_map_assoc($return));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment