Skip to content

Instantly share code, notes, and snippets.

@jeremycaldwell
Created August 30, 2013 22:51
Show Gist options
  • Save jeremycaldwell/6395053 to your computer and use it in GitHub Desktop.
Save jeremycaldwell/6395053 to your computer and use it in GitHub Desktop.
Drupal 7 - Search block alter
/**
* Implements hook_form_FORM_ID_alter().
*/
function THEMENAME_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
// Search block form
case 'search_block_form':
// Change title for search and hide it
$form['search_block_form']['#title'] = t('Search');
$form['search_block_form']['#title_display'] = 'invisible';
// Button
$form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/search-button.png');
// Add extra attributes to the text box
$searchtext = 'Search intranet';
$form['search_block_form']['#default_value'] = t($searchtext);
$form['search_block_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = '$searchtext';}";
$form['search_block_form']['#attributes']['onfocus'] = "if (this.value == '$searchtext') {this.value = '';}";
// Prevent user from searching the default text
$form['#attributes']['onsubmit'] = "if(this.search_block_form.value=='$searchtext'){ alert('Please enter a search term'); return false; }";
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment