Create Image Submit Button with hook_form_alter in Drupal
1 CommentsWith most of the sites i built there has been a need for me to use customised submit buttons. While some of them were fine with just CSS, some required image buttons to be used. Drupal form api doesn't support input submit button type as image, so i had to use the help of PHPtemplate overrides and hook_form_alter to achieve this.
Below is the Snippet for the PHPtemplate file :
<?php
function phptemplate_button($element){
// Make sure not to overwrite classes.
$element['#attributes']['class'] .= ' form-'. $element['#button_type'];
return '<input type="'.(isset($element['#button_type']) ? $element['#button_type'] : "submit").'" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ') .'id="'. $element['#id'].'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
}
?>
Here's a version of the hook_alter_form to use if you prefer that to intercepting the form at the template level rather than using phptemplate_search_theme_form:
<?php
function mymodule_form_alter($form_id, &$form){
if ($form_id == 'search_theme_form'){
$form['submit'] = array(
'#type' => 'submit',
'#theme' => 'button',
'#button_type' => 'image',
'#value' => t('Go'),
'#attributes' => array('src' => 'search_go.gif')
);
}
}
?>
- jay boodhun's blog
- Login or register to post comments









Hey - I am really glad to find this. Good job!