My Drupal theme (Nexus) doesn't have a search bar in the header. So added a custom header in page.tpl.php using the following code.

 <?php if ($search_box):?><div id="searchbar_right" style="float:right"><?php print $search_box ; ?></div><?php endif; ?>

I wish to modify the search-box a bit, so I added the following lines to nexus_form_alter function in template.php.

if ($form_id == 'search_block_form') {
    print $form_id;
    //$form['search_block_form']['#title'] = t('Search'); // Change the text on the label element
    $form['search_block_form']['#title_display'] = 'invisible'; // Toggle label visibility
    $form['search_block_form']['#size'] = 20;  // define size of the textfield
    $form['search_block_form']['#default_value'] = t('Search Site'); // Set a default value for the textfield
    $form['actions']['submit']['#value'] = t('GO!'); // Change the text on the submit button
    $form['actions']['submit']['#attributes']['alt'] = "Search Button"; //add alt tag
    unset($form['actions']['submit']['#value']); // Remove the value attribute from the input tag, since it is not valid when input type = image

    //$form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/icon-search.png');

// Add extra attributes to the text box
    $form['search_block_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = 'Search Site';}";
    $form['search_block_form']['#attributes']['onfocus'] = "if (this.value == 'Search Site') {this.value = '';}";

But the above piece of code doesn't have any effect on the search box that appeared on the top. I suppose that since I have manually printed the search box at the header, hook_form_alter may not be of any use to me.

Can anybody suggest how to change my search-box characteristics.

  • Looks right to me...I'm going to trot out the old standard: did you clear cache? :) You could go into configuration > performance and turn off page and block caching temporarily to try to ensure you see any changes. – Aaron Aug 6 '15 at 20:17
  • @Aaron: I have already tried cleared the cache, will try the other step .. – sjsam Aug 7 '15 at 2:17
  • Look here drupal.org/node/154137 – ar7max Aug 16 '15 at 17:10

I have finally resolved this one using a jquery which I have applied to the element using JS Injector Module.

jQuery(document).ready(function($) { 
  $('#edit-keys').attr('size', '20'); 
  $('#edit-keys--2').attr('size', '20'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.