Last updated 18 April 2008. Created on 18 April 2008.
Edited by aerodog. Log in to edit this page.

Imagine that you'd like, with Drupal's core search module, to jump straight to a node titled, say, "Spinal Tap" by just typing it, rather than having to sift through a bunch of search results. At the same time, you'd like search results when the exact title does not exist among your existing content.

I had this problem and seemingly solved it this way, though your feedback is appreciated for this Drupal noob in case I goofed up.

In search.module, you will find:

>
function search_box_form_submit($form_id, $form_values) {
	return 'search/node/'. trim($form_values[$form_id .'_keys']);
<

Replace it with the following to first check whether that title exists in your data base, and then to go to the URL of that title, here prefaced by movies/. Otherwise, just go ahead and provide search results as you would anyway:

> 
function search_box_form_submit($form_id, $form_values) {
	$temp_result = db_query("SELECT n.title FROM {node} n WHERE n.title='%s'", $form_values['search_theme_form_keys']);
	$temp_object = db_fetch_object($temp_result);	
	if($temp_object->title){
		return 'movies/' . $form_values['search_theme_form_keys'];
	}
	else{
	return 'search/node/'. trim($form_values[$form_id .'_keys']);
	}
<

Of course, this isn't confined to titles. All you'd have to do is replace n.title above with n.whateverfieldyouwanthere

Things get even juicier when you use this with the Search AutoComplete module (http://drupal.org/project/search_autocomplete), where existing titles are suggested as you type.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

VM’s picture

core modules should never be hacked. create a seperate module, create a snippet to do this type of task.

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

wgroth2’s picture

?
-----
Bill Roth, http://loglogic.com/