Created
August 30, 2013 22:51
-
-
Save jeremycaldwell/6395053 to your computer and use it in GitHub Desktop.
Drupal 7 - Search block alter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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