Hello,

Is there a solution to redirect any query made using the standard search form to the views_fastsearch page, that would list the results of the search query?

Example: I search for "concert" in the search box, and "concert" is used as a filter to display the views_fastsearch page.
Then I can perform a new search using the form provided on the views_fastsearch page.

Any idea ?

Comments

douggreen’s picture

Status: Active » Closed (fixed)

Yes, it's pretty easy. Add this to a custom module, it assumes that you've added the standard views_fastsearch view from the VFS module.

/**
 * Override drupal_get_form('search_theme_form') to return a form that the
 * search/views_fastsearch page and the view filter search
 */
function search_theme_form() {
  $form['filter0'] = array(
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search Site'),
  );
  $form['#action'] = url('search/views_fastsearch');

  return $form;
}

I've only tested this in conjunction with views_filterblock.

BobLouis’s picture

Drouggreen, thank you for your response.
What do you mean by "Add this to a custom module"? Could you explain how to do that (or where I could find some info on it)?

BobLouis’s picture

Would the function you propose work if I pasted it into my template file ?

Jo Wouters’s picture

The solution in #1 only works if you use method='get':
So this code should be added:

$form['#method'] = 'get';
PeterZ’s picture

This is what worked for me:

1) create a new folder in your modules folder. I called mine defaultsearch.

2) In the defaultsearch folder, create a file named defaultsearch.info

3) In the defaultsearch.info file put:
; $1$
name = Default Search
description = "Change Default Search to Views Fast Search."
dependencies = views_fastsearch
package = "Views"

3) In the defaultsearch folder, create a file named defaultsearch.module

4) In the defaultsearch.module file put:

// $1$

/**
* Override drupal_get_form('search_theme_form') to return a form that the
* search/views_fastsearch page and the view filter search
*/
function search_theme_form() {
  $form['#method'] = 'get';
  $form['filter0'] = array(
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search Site'),
  );
  $form['#action'] = url('search/fast');

  return $form;
}

6) Go into your administer >> modules, and enable the new defaultsearch module which will be in the Views section.

You're basically creating your own new module (this is my first one). More information about creating modules is at http://drupal.org/node/82920.

scottrigby’s picture

Version: 5.x-1.1 » 5.x-2.0

Hi Doug & Peter,
I followed the instructions in #5 exactly – sadly this did not work for me...
could it be that I'm missing some vital step?

I have views_fastsearch installed

I 'added' the default view that came packaged with the module 'views_fastsearch'
The only changes I made to that were:
* I followed the configuration instructions here: http://www.lullabot.com/articles/custom_search_forms_views_and_fastsearch
* and then I changed the View Type from Search Results to 'Teaser List'

I can confirm that my custom view appears correctly at mysite.com/search/fast

But no redirecting is happening, either when I search from mysite.com/search or from the 'Search' block

** What I really want to do is expose filters on the standard search block (which I have in my header), just like the lullabot tutorial has us expose filters on the custom fastsearch page page.
(the reason I'm trying the solution proposed by Doug above is that I also need the search to redirect to the proper results page).

Thanks in advance for any insight or suggestions about this!
:) Scott

PS, I'm using 5.x-2.0 ... could that be the problem? Should I revert back to 5.x-1.1?

scottrigby’s picture

PPS, as a long shot, I tried to add this view as a block and that also did not show anything...

G.I.Joe’s picture

change the function search_theme_form() in the defaultsearch.module

function defaultsearch_form_alter($form_id, &$form) {
if($form_id == 'search_theme_form'){
  $form['#method'] = 'get';
  $form['filter0'] = array(
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search Site'),
  );
  $form['#action'] = url('search/fast');

  return $form;	
}
}
Alex72RM’s picture

The really simplest way (and more brutal)?

search.module, search for search_box_form_submit function, comment a line and insert another one.

function search_box_form_submit($form_id, $form_values) {

  //return 'search/node/'. trim($form_values[$form_id .'_keys']);
  drupal_goto('search/fast', 'filter0='. trim($form_values[$form_id .'_keys']));

}
doc2@drupalfr.org’s picture

Marked #245318: Display Search Box on front page. as duplicate.

Edit: Next poster please reopen this issue to keep it "active" so that we go on sharing best practices.

mbelos’s picture

To #9: Hacking core code is a really messy way to do this. What happens when you update Drupal? Your changes will break, and your search form won't work any more.

I used #8's solution, but I had to override the 'search_block_form' rather than 'search_block_theme':

function defaultsearch_form_alter($form_id, &$form) {
	//search_theme_form
	if($form_id == 'search_block_form'){
	  //Kill the old elements in the form
	  unset($form['search_block_form_keys']);
	  unset($form['submit']);
	  unset($form['#action']);
	  unset($form['form_id']);
	  unset($form['form_token']);
	  
	  $form['#method'] = 'get';
	  $form['filter0'] = array(
	    '#type' => 'textfield',
	    '#size' => 15,
	    '#default_value' => '',
	    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
	  );
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('Search'),
	  );
	  $form['#action'] = url('search');
	  return $form;	
	}
}

The only thing I have now is an ugly '&op=Search' at the end of the URL. Other than that, this works fine with Drupal 5.

PeterZ’s picture

I found another way to do this that I'm using in Drupal 6. I'm doing a URL rewrite in my .htaccess file. Mine looks like:

RewriteRule ^search/node/(.*)$ procedures&keys=$1&tid=All [NC,L]

Based on my understanding, the parts are:
^ I think this basically gets the base url
search/node/ is the default core search URL
(.*)$ is the keyword the user typed in (which shows up at the end of the URL in D6)
procedures is the path of my view
&keys= is the name of my first filter (the search keyword)
$1 repeats the same text that was found in (.*), which is the keyword
&tid= is a second filter I have on the page that doesn't default to all
All is what I want the second filter to default to

You'll have to adjust for your case.

I expect this would work in D5 also.

At least it doesn't change core. But it will break if I change the path of the view or the default path of search. So it has other maintainability issues.

davemybes’s picture

Using the 'get' method results in extra junk after your search term. After much searching and hacking, I worked a very simple method to redirect a core search query to another URL. Add the following to a custom module (change MODULENAME to the name of your module):

function MODULENAME_form_search_theme_form_alter(&$form, &$form_state) {
  $form['#submit'] = array('MODULENAME_custom_search_submit');
}

function MODULENAME_custom_search_submit($form, &$form_state) {
  $form_id = $form['form_id']['#value'];
  $form_state['redirect'] = 'search/fast/' . trim($form_state['values'][$form_id]);
}

Note that this can work for any search, just change the redirect path e.g. for Faceted Search, use your environment's Base path followed by '/results/'.