Fixed: Google web search block

29 Aug 2007


As described earlier, I've placed many a Google web search block into Drupal sites, using Google AdSense for Search.

All to a single end: the blocks don't work. (Hey, consistency counts for something, right?)

As I detailed, both Google and Drupal (even with clean URLs implemented) want to use q in the search parameters toward conflicting ends, resulting in goofiness. One suggested solution is to replace the problematic q in the search code with as_q, something Google and Drupal will both accept but not fight over. (Saintly correspondent jGirlyGirl informs me that query works as well.)

Alas, my problems only half-disappeared: with the code tweak, on-site searches worked fine, but web searches would only return "Your search... did not match any documents."


I hacked much yesterday, trying all sorts of little tweaks to the search code. I then came up with the brilliant strategy I should have thought of weeks ago, which was to check out some sites with working Google search blocks. I even hit upon the perfect candidate: the excellicious 43 Folders site by burgeoning Internet hero Merlin Mann.

That site not only employs an AdSense for Search block for both on-site and web searches, but it's a Drupal site as well! (Let 43 Folders, not The Onion, be our new poster child for Drupal sites.)

With that specimen laid out before me, I sought to employ the single most valuable technique known to me as a programmer. That's right: copying other people's code.

I viewed the source code for 43 Folders' search results page, and compared with my own. Hmm, 43 keeps the unadorned q in its search parameters; apparently, Merlin solved that problem at the .htaccess level or elsewhere. Yet as_q should still work for me, as it has for many others. I see, too, that 43's code specifies its search results page with a trailing "/", while my code does not. Was that the problem? No, tacking a slash onto the end of the page URL in my code didn't fix things (in action, the slash just gets stripped out of the resulting URL anyway).

And so on. I noted every difference I could find. I even went as far as nabbing 43's search code wholesale and pasting it into my Drupalace search block, changing only the obvious particulars like my domain, my search result page URL, etc. But every variation I tried resulted in some error: either a "Your search... did not match any documents" result or a "page not found" result, for on-site search, web search, or both.

"Why will this same code work for him, but not for me? Aaaarrgggghhh!!!!"

The workable, if gustatorily unsatisfying, solution

In the end, I went back to the Google AdSense setup page, and grabbed fresh code for my block. And it works.

Wait, don't laugh at me yet. Here's the rub:

I earlier pointed out how all my block seemed to work at first, and at some point stopped. I can now elaborate on that. It appears that when I paste fresh code into a block (and change q to as_q), it works – but if I then edit that block again for any reason, the code breaks. And unfortunately, I have to edit the code. First, I need to change the q as above, if I forgot to do so on the first pass. Then I'll see that my on-site search is by default set to the full domain ("www."), which makes the block too wide, so I'll return to change that to something shorter like "This site". Then I'll say "oh, yeah, I forgot to give the block a heading", so it's back to edit that.

Touch the block, and it breaks. I don't know why. The first suspect is input format – the code will obviously break if I use Filtered HTML or any other format that futzes with the code. But I use an Unfiltered HTML format, which should – and appears to – leave code unmolested (other than stripping line breaks).

So I don't know why editing the block breaks code, even when I only edit, say, the block header and don't touch the code. But I do know now how to make things work:

When I prepare the block, I make all changes at once: paste in the code, change its q to as_q, change its search options to read "This site" instead of the full domain name, and give the whole block a header (if I want one). The I save the block and don't edit it again. If I do have to edit it again, I paste in (and tweak) fresh code at the same time.

Problem ameliorated, if still marred by the above remaining annoyance. And as a denouement to my tale, my solution's a boring one too. But if that lengthy overview helps anyone else with the same issue, I'll be a happy Ace.


Add new comment