Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How To: Add focused Drupal search to your site

Parent Feed: 

Play Video

Movie link (right-click to download) Problem with this video? Contact me

When it comes to search, Drupal seems to do OK by itself. However, there are a number of supporting modules which will make your Drupal's default search even better. These include Porter-Stemmer (english only), Search 404, Search by Page, Similar By Terms and many others.

If you're seekign to help an advanced user out, then modules like Search config can help with that. But what about the user who won't dare go into the hidden area of 'Advanced Search'? This is where the power is - right?

It sure is. This is where you tell Drupal what content types and categories you want to limit the search to. This is where a user, simply looking for a job on your site, which lists information about jobs, news, blogs and other items, can focus their results.

So, why don't you stop expecting the user to figure this out, and just make it happen for them! That's what this video is all about. Using the default Drupal search box and forcing it to focus on specific content types or categories. You control what Drupal searches for and you control where it shows up!

Search Related Modules

The Apache Solr project is what is used on Drupal.org. You know, where you get the cool faceted results from a search request.

Of course, if you haven't heard about it already, the Acquia Search service make it quite easy to take advantage of the Apache Solr coolness.

Here's the code.

<?php
// All of the following code goes within template.php

/*
* Using one of the preprocess hooks in template.php, you create a
* variable which calls drupal_get_form on the function which creates
* the search form
*/

function THEME-NAME-HERE_preprocess_node(&$vars, $hook) {
 
$vars['focused_search'] = drupal_get_form('THEME-NAME-HERE_focused_search_form');
}
/*
* This function will create a search form which adds a validation
* handler which you use to append any custom search criteria
* Note to self: Context module could be used here?
*/
function THEME-NAME-HERE_focused_search_form(&$form_state, $keys = '') {
 
// Render the form to search a focused content type
 
if( module_exists('search')) {
   
$form = search_form($form_state, '/'. drupal_get_path_alias($_GET['q'])); // Use existing search form code
   
$form['#validate'] = array('search_form_validate', 'THEME-NAME-HERE_focused_search_validate');
   
$form['#submit'] = array('search_form_submit');
    return
$form;
  }
}
/*
* The validation handler added within the search form is where you make
* any adjustments to the search handled by Drupal. You can use any
* critera you wish, including the path or other variables available
* within Drupal.
*/
function THEME-NAME-HERE_focused_search_validate($form, &$form_state) {
 
// Using an array to determine which content type to search based on path from search form
 
$content_type = array_search($form['#action'], array(
                 
'vendor_item' => '/vendors',
                 
'job_item'    => '/jobs/sites',
                    )
                  );
 
$content_type = empty($content_type) ? '' : $content_type;
 
// Initialize using any existing basic search keywords.
  // taken from node.module node_search_validate() function
 
$keys = $form_state['values']['processed_keys'];
 
$keys = search_query_insert($keys, 'type', $content_type); // additional seach criteria added here
 
form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state);
}
?>
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web