Pages

Friday, January 28, 2011

Drupal 7 Google Custom Search Engine

As of right now, the Google Custom Search Engine Module is not available for Drupal 7. Now in many people's opinion, Drupal's core search, well in one word, sucks. For me, I am impartial. I like both Drupal's core search and also like the auto-completions and better advertising of the Google Custom Search Engine(CSE). But for those who like using Google's CSE and have taken the plunge into Drupal 7 might have come across an issue with using the code directly from google.com/cse.

The problem that many people have faced with using Google CSE is that when using the iframe look and feel, the search box code uses GET and the variable q to transfer what the user is searching for to the custom page you create with the search results code on it. But the variable q is also used by Drupal, so when your search box redirects to your custom page with the q variable set to the search criteria, Drupal breaks usually saying the page does not exist. But there is a very easy way around this problem.

The solution to the Google CSE problem is to change q in the search box code to as_q. If you go here, you can see that q and as_q do exactly the same thing and Google parses them the same when using it's search. And since Drupal does not use the as_q variable via GET, the search page should work fine.

Here are the exact steps if needed:
1. Set up your Google Custom Search Engine at http://www.google.com/cse
2. In the "Look and Feel" section, select iframe
3. Create a page on your drupal site for the search box to go to, such as site.com/customsearch
4. Using PHP as the input mode, copy and paste the Search results code from the Get Code section
5. Create a new block and using the PHP input mode, copy and paste the Search box code from the Get Code section into the block.
6. Change the line that looks like this document.getElementById('q') to this document.getElementById('as_q')
7. Change the line that looks like this <input autocomplete="off" id="q" name="q" size="31" type="text" /> to this <input autocomplete="off" id="as_q" name="as_q" size="31" type="text" />

2 comments:

  1. Is there anyway to get the Drupal 7 version to display results in another block?

    I have the search box in the header section, but I want to add the results in the Featured section.

    Is it a matter of dropping the google_cse_results_tpl.php into the Featured block. How would I do this?

    ReplyDelete
  2. Thanks a lot for this post !!

    Was enough to do until step 4 for me.
    drupal

    ReplyDelete