I am posting this more as information, since it took me a while to figure out the best way of doing this.
What I wanted to do was replace the plain 'submit', 'preview' and 'delete' buttons on a content edit form with images. Previously I had done by overriding theme_button and changing the output for certain buttons ... this didn't work so well in Internet Explorer and there were problems with multiple buttons (See http://drupal.org/node/62647.
Drupal 6 introduces the image_button type ... which was clearly a starter. The only real gotcha was that it doesn't work if the #value for a button is set and there are multiple buttons. The fix for that was here: http://drupal.org/node/359529
To change the buttons you need to implement hook_form_alter, play 'find the button' and then:
1. change it's type.
2. Set the #src to the appropriate image
3. Set the #value property to Null so that the image_buttons post properly.
/**
* Implementation of hook_form_alter().
*/
function thegardenwall_form_alter(&$form, $form_state, $form_id) {
$img_path = path_to_theme().'/images/buttons/';
// If there are any buttons then lets alter them
if ($form['buttons']){
// Is there a submit button
if ($form['buttons']['submit']){
$form['buttons']['submit']['#type'] = 'image_button';
$form['buttons']['submit']['#value'] = null;
$form['buttons']['submit']['#src'] = $img_path.'btn_save.gif';
}
//return;
// Is there a delete button
if ($form['buttons']['delete']){
$form['buttons']['delete']['#type'] = 'image_button';
$form['buttons']['delete']['#value'] = null;
$form['buttons']['delete']['#src'] = $img_path.'btn_delete.gif';
}
// Is there a preview button
if ($form['buttons']['preview']){
$form['buttons']['preview']['#type'] = 'image_button';
$form['buttons']['preview']['#value'] = null;
$form['buttons']['preview']['#src'] = $img_path.'btn_preview.gif';
}
}
}
Comments
Actually it doesn't work!
It treats it as a form submit no matter which of three buttons I click ... and am at a loss to figure out what I am doing wrong
same problem
same problem
Same here. I also tried
Same here.
I also tried playing with #executes_submit_callback but it didn't work so far.
-------------------
Alon Peer
Web development
http://alonpeer.com
This worked for me
Having just fought with this for a while, and not having found clear documentation, I'm adding what seem like the key points for using multiple image_buttons in a form:
1) Do give each image_button a unique #name (unlike regular buttons, you can't have several image_buttons all named op).
2) Don't give an image_button a #default_value.
3) Don't give an image_button a #value
4) Do give each image_button a #return_value. This is the same as what you would assign to #value if IE6 problems hadn't forced Drupal to implement some unusual element handling. If you don't assign this, it will default to true.
5) Do use $form_state['clicked_button']['#value'] to find the #value of, well, the clicked image_button. This field will contain the value of #return_value for the clicked image_button.
CraftySpace.com Better Websites for a Better World
example??
example code is always nice.. :)
i don't get #5 - where do i see $form_state['clicked_button']['#value']?
are you suggesting:
Peter Lindstrom
LiquidCMS - Content Solution Experts
this works for std node form
this works:
Peter Lindstrom
LiquidCMS - Content Solution Experts
works, with #return_value
This also work (with and without define #name key). Also, work with define separate function for each button in key '#submit'. Main thing is to define '#return_value'.
Where?
Where do I put this code to edit the button? Do I need to edit the module itself or can this be done in template.php or something similar?