I'm trying to replace the search button in the search form with an image instead. I'm using this code within a small module:

function custom_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'search_theme_form') {
     $form['submit'] = array('#type' => 'image_button', '#value' => t('Search'),
                             '#src'  => 'sites/all/themes/fusion_starter/images/search_go.jpg');
  }
}

Any idea what I'm doing wrong? I'm printing the search box on my page.tpl.php file using print $search_box. Thanks

Comments

bkosborne’s picture

Surely people have done this before..?

jclaussen’s picture

Is the module name 'custom.module'?

Have you checked to make sure your function is getting called?

Do you need to add a 'return'?

my example:

function custom_form_alter(&$form, $form_state, $form_id) {

switch ($form_id) {
case 'search_theme_form':
$form[$form_id] = array(
'#title' => t('Search this site'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => 'Search',
'#attributes' => array('title' => t('Enter the terms you wish to search for.')),
);
$form['submit'] = array('#type' => 'image_button', '#value' => t('Search'),
'#src' => 'sites/all/themes/30rock/assets/arrow.png');

break;
}

return;
}

bkosborne’s picture

I modified it to read this:

<?php

function custom_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
  	case 'search_theme_form':
    	$form['submit'] = array('#type' => 'image_button', '#value' => t('Search'),
                             	'#src'  => '/sites/all/themes/fusion/fusion_starter/images/search_go.jpg');
			break;
	}
	return;
}

And yes, the module is called custom. I know not a great name, but I'm just trying to get this to work. It is being invoked as well. Why do I need to put in the the code you entered? Looks like that pertains to the actual searchbox but I'm just looking to overwrite the search button. Thanks

bkosborne’s picture

This is driving me nuts! I tried even just changing the text from "Search" to "Go" using:

function custom_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
  	case 'search_theme_form':
			$form['submit']['#value'] = t('Go');
			break;
	}
}

that doesn't work either. I know that it's actually modifying the form variable because I'm printing the array to firephp after I change it and the change is there. I don't need a return statement because the form variable is passed by reference. Can anyone please help me with this? I cleared the cache as well to no avail...

jhubley’s picture

If you're still looking for an answer, you might try:
http://drupal.org/node/755200

bkosborne’s picture

Turns out there was a hidden template that was superceding the hook!

jhubley’s picture

Aha. Glad you figured it out!