Community Documentation

Styling Drupal 5.x search forms

Last updated May 3, 2010. Created by Wolfflow on April 24, 2010.
Edited by arianek. Log in to edit this page.

All changes should be tested on a development site before being applied to a production site.

This example involves creating a theme function named mytheme_search_form(). Create the function as follows:

<?php
function mytheme_search_form($form){
  return
_phptemplate_callback('search_theme_form', array('form' => $form));
}
?>

Then create a file in the theme directory named “search_theme_form.tpl.php” and build a custom form.

Note: The following procedure is not well-understood and appears to be a hack.

To see an example of adding additional fields to the search form, look at the function node_form_alter() in node.module. It doesn’t seem necessary for us to alter the form so extensively since the form is hard-coded in search_theme_form.tpl.php, so we just add some radio buttons with the name “category” so that we can filter search results by taxonomy. But this doesn't work: the search will never actually filter on results. After some investigation, we go back to node_form_alter(), noticing a “processed_keys” key in the $form array. So to make the form honor category search, we add the following code to your template.php file.

<?php
function mytheme_search_keys($type = null) {
 
$keys = search_get_keys();
  if(
$type) {
   
$keys = search_query_insert($keys, 'type', $type);
  }
  if(
$_POST['category']) {
   
$keys = search_query_insert($keys, 'category', $_POST['category']);
  }
  return
$keys;
}

/* Taking over this function so that we can call mytheme_search_validate to do the advanced search.*/

function blog_form_alter($form_id, &$form) {
  if(
$form_id == 'search_form') {
   
$form['#validate']['mytheme_search_validate'] = array();
  }
}

/* Need to call this to add category to the processed_keys array item so that
* the category actually gets searched in the mini-advanced form we generate
* in mytheme_search_form().
*/

function mytheme_search_validate($form_id, $form_values, &$form) {
 
$keys = $form_values['processed_keys'];
 
$keys = mytheme_search_keys($form_values['module']);
 
form_set_value($form['basic']['inline']['processed_keys'], $keys);
}

function
faq_search($op = 'search', $keys) {
  switch(
$op) {
    case
'name':
      return
t('content');
    default:
     
$keys = mytheme_search_keys('faq');
      return
node_search('search', $keys);
    }
}

function
forum_search($op = 'search', $keys) {
  switch(
$op) {
    case
'name':
    return
t('content');
  default:
   
$keys = mytheme_search_keys('forum');
    return
node_search('search', $keys);
  }
}

function
blog_search($op = 'search', $keys) {
  switch(
$op) {
    case
'name':
      return
t('content');
    default:
     
$keys = mytheme_search_keys('blog');
      return
node_search('search', $keys);
    }
}
?>

Now for an explanation. The mytheme_search_keys() function is a helper that limits the search to a given node type. It's not clear if it is absolutely necessary to add the “type:” string to the search, but things don't seem to work without it, so we are leaving it in. Note that this function also looks for $_POST['category'] and adds it to the keys. If we wanted to add other search fields, we would add them here as well. The function mytheme_search_validate() calls mytheme_search_keys() to set keys.

Next up, blog_form_alter(). The hook_form_alter() functions are associated with modules, so we chose one we knew our site would be using that didn't have a form_alter hook defined already. It feels kind of hacky, but it seems to work. The idea here is that we need to make the form run a validation function in order to add the keys we’re pulling in from mytheme_search_keys(). The category filter finally started working after adding this function, so it seems to be crucial. Adding the keys to the $form['basic']['inline']['processed_keys'] array item seems to handle adding the search criteria to the URL and to the search itself.

Finally, we added the three _search() functions, which again feel a little extraneous, but it doesn't work unless we add them, so they're staying put for now. All we're doing in these functions is adding the node type to the keys being searched (the search code extracts things like “type:blog” and “category:3? from the query to do advanced searches) and then executing the node_search() function with the revised $keys value.

So, there you have it, the long way around to having a custom-themed Drupal search form with additional filters based on node type and category.

Page status

About this page

Drupal version
Drupal 5.x
Audience
Designers/themers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.