How to display the "Search" label on the block search form in Drupal 7

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

This is just a quick tip on something I was trying to do earlier. I couldnt find anything in a search, so I thought I'd document what I found out here, in case others are looking. I might make this into a proper article some other time - it could be a good intro to theming forms.

By default, Drupal 7 hides the <label> element on the search form by applying an "element-invisible" class. This is the CSS that class applies:

.element-invisible {
    clip: rect(1px, 1px, 1px, 1px);
    position: absolute !important;
}

Trying to override this with CSS is a bit messy. The ideal solution is to remove the element-invisible class. To do this, you need to customize the search block form using the hook_form_alter function in your template.php file. There is some documentation on this here:

http://drupal.org/node/154137

The value you need to change is title display. To put the title before your search form, use the following code:

<?php
function YOURTHEME_form_search_block_form_alter(&$form){
   
$form['search_block_form']['#title_display'] = 'before';
}
?>

The possible values for title_display are before, after, invisible, and attribute (attribute doesn't work for text fields, so ignore that for now).