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

irinaz created an issue. See original summary.

stefan lehmann’s picture

Yep. 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!

:-)

irinaz’s picture

Congratulations on the second baby! Let me know if I can do something to help add code :)
Thanks, Irina

  • Stefan Lehmann committed 04d6c8b on 7.x-1.x authored by irinaz
    Issue #2853916 by irinaz: Do not show nodes that are already included in...
stefan lehmann’s picture

Assigned: Unassigned » stefan lehmann
Status: Active » Fixed

*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:

if ( substr($string, 0,3) == "EQ ") {
  $query->condition('title', substr($string, 3), '=');
}

.. 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.

irinaz’s picture

Stephan,
thanks a lot! It is perfectly fine to drop those lines!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.