Out of the box in autocomplete field "Node title lookup" module shows all nodes that contain search string, including nodes that are already excluded from search in field "Node ids to be excluded from search result". This becomes a problem if we need to exclude lots of nodes with similar names, for example "coffee break".
we have added small helper module that fixed that problem.
/**
* Autocomplete helper, $string = string to search for.
*/
function custom_search_exclude_nid_nodes_autocomplete($string) {
$matches = array();
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'));
if ( substr($string, 0,3) == "EQ ") {
$query->condition('title', substr($string, 3), '=');
}
else {
$query->condition('title', '%' . db_like($string) . '%', 'LIKE');
}
$excluded_nids = variable_get('search_exclude_nid_search_exclusion_nids', array());
if (count($excluded_nids)) {
$query->condition('n.nid', $excluded_nids, 'NOT IN');
}
$result = $query->execute();
// Save the query to matches.
foreach ($result as $row) {
$matches[$row->nid] = check_plain($row->title) . ' ( ' . $row->nid . ' )';
}
// Return the result to the form in json.
drupal_json_output($matches);
}
Comments
Comment #2
stefan lehmannYep. that sounds like a sensible idea. I'll add that soon, but I'm a bit short on time at the moment as we just had our second baby.
Thank you for the suggestion and code anyway!
:-)
Comment #3
irinaz commentedCongratulations on the second baby! Let me know if I can do something to help add code :)
Thanks, Irina
Comment #5
stefan lehmann*cough* "Soon" as in 8 months later of course.
The baby is now a little girl. :-)
Anyways I took over most of your code, apart from:
.. which for sure is helpful for some. But I don't want to blow up that functionality with features which only 0.1% actually need. I hope you can live with that.
Comment #6
irinaz commentedStephan,
thanks a lot! It is perfectly fine to drop those lines!