Create Image Submit Button with hook_form_alter in Drupal

Create Image Submit Button with hook_form_alter in Drupal

10 December 2009 by jay boodhun Create Image Submit Button with hook_form_alter in Drupal 1 Comments

With 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')
    );
  }
}
?>

Super-Duper site! I am loving it!! Will come back again - taking your feeds too now, Thanks.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Syntax highlight code surrounded by the {syntaxhighlighter OPTIONS}...{/syntaxhighlighter} tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.