Drupal7 - Customize the search form

Here is the code to be used in the theme's template.php file that will override Drupal's search form.
If no template.php is available, just go ahead and create one.

<?php
function theme_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == 'search_block_form') {
  
   
$form['search_block_form']['#title'] = t('Search'); // Change the text on the label element
   
$form['search_block_form']['#title_display'] = 'invisible'; // Change the label's visibilty
   
   
$form['search_block_form']['#size'] = 40// Here a custom size for the textfield can be provided.
   
$form['search_block_form']['#default_value'] = t('Search'); // Set a default value for the textfield
   
$form['actions']['submit']['#value'] = t('Search Now!'); // Change the text on the submit button
   
$form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/search.png'); // Change with an image
 
    // Add extra attributes to the text box so that the word Search will show up in the textarea, and hide on click
   
$form['search_block_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = 'Search';}";
   
$form['search_block_form']['#attributes']['onfocus'] = "if (this.value == 'Search') {this.value = '';}";
  }
}
?>

The above snippet was taken

Tags: 
Drupal 7, Drupal Quick Tips

Recent Comments

Forward by Dries Buytaert, Founder and Project Lead, Drupal; CTO Acquia.

Order now from:

Managing Expectations Podcast with DrupalEasy

Author Interview http://tinyurl.com/8a8nxno

We provide the most Shiny Drupal Bits & Pixels

Since Drupal 4.x