Using Google CSE on Drupal

 The goal was to use Google CSE on drupal, and while I got it working it was not near as easy as I would have liked.

At first it seemed simple, google provides two pieces of code; One to build the search form, and one to display the results within your own page.

I added a block for the Google search form which was quite simple. After creating the block I removed Drupals search block and added the new Google search block. (On creating the search engine I gave it the url of http://cross-tags.com/search-result for the results to appear).

I then created a plain page entry with a url alias matching what I had supplied to the Google Custom Search Engine for diplaying of results. I gave the page not content other than the code required to display the search results.

When visiting the page itself all seemed right, the source code contained the required code from Google. But when using the search form got a 404 error.

The problem was that Googles search form use q=search term , Drupal use the q= also and would always return a 404 unless the search was 100% matching on an existing page.

Now my alternatives was to build a custom page seperate from Drupal to handle search results. I didn't really want to do this as then I lose the ability to manage / theme it with Drupal, and would require seperate work on each theme / block change.

The only option that I have found so far involved creating a module (probably not needed but wanted a custom tpl file for this page) that just served up a custom template / page for the url I was wanting and hacking into the core (common.inc).

I had to put a test into the 404 handler in /includes/common.inc and test to see if the request was for search

/**
 * Generates a 404 error if the request can not be handled.
 */
function drupal_not_found() {
  //test if this is a search request
  //the search forms uses sa=Search
  //So if this is a 404 and sa=Search then must be a Google search from request
  if (isset($_GET['sa']) && $_GET['sa'] == 'Search'){
    $path = drupal_get_normal_path('search-results');
    $return = menu_execute_active_handler($path);
    print theme('page', $return,TRUE); // set to TRUE to display blocks on the search results page
  }else{
    drupal_set_header('HTTP/1.1 404 Not Found');
    watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
    $path = drupal_get_normal_path(variable_get('site_404', ''));
    if ($path && $path != $_GET['q']) {
      // Set the active item in case there are tabs to display, or other
      // dependencies on the path.
      menu_set_active_item($path);
      $return = menu_execute_active_handler($path);
    }
    if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
      drupal_set_title(t('Page not found'));
      $return = t('The requested page could not be found.');
    }
    // To conserve CPU and bandwidth, omit the blocks.
    print theme('page', $return, FALSE);
  }
}

________________________________________________________________________

Now in my google_cse.module

<?php
// $Id$
function google_cse_help($path, $arg) {
//help not really needed but putting it in just because
$output = '';
  switch ($path) {
    case "admin/help#google_cse":
      $output = '<p>'.  t("Displays google cse results") .'</p>';
      break;
  }
  return $output;
} // function google_cse_help
/**
* Valid permissions for this module
* @return array An array of valid permissions for the Google  CSE REsults module
* No administer needed but added just in case
*/
function google_cse_perm() {
  return array('administer Google_CSE','view Google_CSE');
}

function google_cse_menu() {
  $items = array();
  //the url supllied to Google for the form building http://cross-tags.com/search-results
  $items['search-results'] = array(
    'title' => 'Search Results',
    'description' => 'Google Search Results',
    'page callback' => 'google_search_results',
    'access arguments' => array('view Google_CSE'),
    'type' => MENU_CALLBACK,
  );
return $items;
}

function google_search_results(){
  //An admin function to allow a Drupal admin to input this code planned
  $output = '
<div id="cse-search-results"></div>
<script type="text/javascript">
  var googleSearchIframeName = "cse-search-results";
  var googleSearchFormName = "cse-search-box";
  var googleSearchFrameWidth = 800;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>';
return $output;
}
function google_cse_preprocess_page(&$vars, $hook) {
  // Since the minimum width for a google search results page is 800 pd
  // I need to provide a custom tpl file to adjust for.
  if (isset($_GET['sa']) && $_GET['sa'] == 'Search'){
    $vars['template_file'] = 'page-search';
  }

I will update this page / or create another if I find better solutions to this problem.

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
13 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.