Theming the Advanced Search Form
This week a client sent me the following screen shot:
The request was to remove the list of content types and change the select list of categories into a set of checkboxes. Totally no problem! I wrote a whole CHAPTER about theming forms in Front End Drupal. I know how this stuff works!
And then I spent an embarrassing amount of time wracking my brain around why I couldn't change the select list to checkboxes. It was easy to do what I wanted by hacking core. And it was trivial to just remove the content types from the theme layer. But for the life of me I forgot that you can't change the type of a form element from the theme layer. You can only alter attributes like the text description and label. Duh!
Once I realized the error of my ways I wrote a tiny little module that:
- Unsets the list of content types.
- Pops the list of terms off the of single vocabulary within the site for display as checkboxes. (Read the Form API page for more info on why you have to do this.)
Here's the steps you'll need to use if you want to replicate the same changes on your site:
- Identify the ID for the form you want to change. View the source of the rendered HTML page and find the "value" attribute for the hidden field named, "form_id." In this case I needed to change "search_form."
- Create a new folder in your site's module directory. For example: mythemename_overrides.
- In the new folder create a file that matches the directory name. e.g. mythemename_overrides.info. Into that file add the following text:
name = Advanced Search Options
description = Alter the display of the advanced search form
dependencies[] = search
core = 6.x
Save and close this file. - Create a second new file named mythemename_overrides.module. Into this file add the following text:
<?php
function mythemename_overrides_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_form') {
// Content types: remove the option to narrow focus based on content types.
unset($form['advanced']['type']);
if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
// Taxonomy: Allows for one vocabulary only to be used in advanced search
$terms = array_pop($taxonomy);
$form['advanced']['category'] = array(
'#type' => 'checkboxes',
'#title' => t('Only in the category(s)'),
'#prefix' => '',
'#suffix' => '',
'#options' => $terms,
);
}
}
}
Save and close the file. - Your module is now ready for use. Go ahead and navigate to admin/build/modules. You'll see the new module listed (Advanced Search Options). Enable the module. Then go visit the search form. The new display should look something like this:
Want More Tutorials?
Are you interested in getting even more tutorials sent to your inbox? Sign up for our incredibly popular tips mailing list and get a free tutorial every month. You may even win a coupon for a free e-book (include your whole name if you want to win stuff).
For a limited time we'll even send you a bonus tips sheet The Top Ten Mistakes People Make when Theming Drupal when you sign up for the list.


