Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created June 14, 2011 18:01
Show Gist options
  • Select an option

  • Save gavinblair/1025461 to your computer and use it in GitHub Desktop.

Select an option

Save gavinblair/1025461 to your computer and use it in GitHub Desktop.
Drupal 6 - make external image_buttons work like D7
<?php
//you could also add this to your theme's template.php file, by replacing "phptemplate_image_button" with "themename_image_button" in the function name.
function phptemplate_image_button($element) {
// Make sure not to overwrite classes.
if (isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
}
else {
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
}
$src_uri = $element['#src'];
$data = explode('://', $src_uri, 2);
$scheme = count($data) == 2 ? $data[0] : FALSE;
if (!$scheme) {
$src_uri = base_path() . '/' . $element['#src'];
}
return '<input type="image" name="'. $element['#name'] .'" '.
(!empty($element['#value']) ? ('value="'. check_plain($element['#value']) .'" ') : '') .
'id="'. $element['#id'] .'" '.
drupal_attributes($element['#attributes']) .
' src="' . $src_uri . '" ' .
(!empty($element['#title']) ? 'alt="'. check_plain($element['#title']) .'" title="'. check_plain($element['#title']) .'" ' : '' ) .
"/>\n";
}
@gavinblair

Copy link
Copy Markdown
Author

@SeanJA

SeanJA commented Jun 14, 2011

Copy link
Copy Markdown

I do wish that they would just use the freaking button element... sigh

@gavinblair

Copy link
Copy Markdown
Author

In my case it's a "Pay with PayPal" button. Image buttons make sense, they are valid HTML, no?

ghost commented Jun 15, 2011

Copy link
Copy Markdown

IIRC inputs typed to image send the coordinates on the image where they clicked, so technically they're sending more data (although not much).
button elements can be assigned as submit buttons natively whereas image types would need JS in order to dispatch submit form on click/enter.

Both are valid. (S/T/F)
Button elements are more useful in most cases.

@gavinblair

Copy link
Copy Markdown
Author

Thanks e1nherjar. I tried disabling javascript in Firefox and click+enter still worked on the image button.

You're right about the coordinates of the click event, in my case it's no biggie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment