Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By profix898 on
function theme_submit($element) {
return theme('button', $element);
}
function theme_button($element) {
return '<input type="submit" ... />\n";
}
I'm unable to create a simple button with form-api! All buttons are automatically of type="submit".
Form.inc contains to functions for theming a button (look above):
1. theme_submit() what creates a type="submit" button. Perfect.
2. theme_button() what also create a submit button. Why?
I think theme_button() should create a type="button" button instead, right?
And let theme_submit() handle the creation of a submit button.
Comments
Much better ...
Sorry. I forgot:
'#button_type' => 'button'
There is an attribute for buttons #button-type. But in form.inc it is completly ignored.
All generated buttons are submit buttons.
I think the code should be:
That creates a submit button in theme_submit() and a button of type #button_type
in theme_button().
Even better
In my version of theme_button(), I have made sure that not every value will be accept for the tag parameter "type". This way, if the type provided by the form element is either "submit", "reset", or "button", then it will be left unchanged since those type exist in the real html world. But if something else is thrown at it, then the html INPUT TAG type will default to "submit".
Live long ... and prosper!
Excellent stuff. Worked
Excellent stuff. Worked great. Can't believe Drupal 5.xs is still ignoring the '#button_type' out of the box.
--
Ixis (UK) providing Drupal consultancy and Drupal theme design.
--
Ixis (UK): Drupal support, Drupal hosting.
Yeah, I just ran into this problem
Yeah, I just ran into this problem while trying to add a button with an onclick attribute. #button_type is completely useless and the problem still persists in version 5.7. I don't know how this slipped past testing.
Rather than hacking the core in form.inc, asiby's override function can be added to template.php as phptemplate_button().
Can you please explain some more..
Can you please explain where and how to write the phptemplate_button() function. I am new to this.
Does it need any parameters?
Thanks!!
-
Cheers!
Sushil H
template.php
The code is inserted into the theme's template.php file, located in the root of the theme directory. The code may need to be massaged, depending on what you're trying to accomplish.
I put this code into template.php of my current theme..
But still taking the buttons have type "submit".
Is there anything wrong on above code or i need to make changes in some other files too?
Sorry if its annoying but i am new to drupal coding. :(
-
Cheers!
Sushil H
Still broken in D7 FAPI - workaround included
I believe too button support should be fixed in the core form.inc.
The workaround that worked for me in D7:
Add the following function in your template's template.php file.
and in your form
this is code for button in my module
$form['social']['go'] = array(
'#type' => 'button',
'#value' => t('Go'),
'#button_type' => 'button',
'#prefix' => '
'#suffix' => '
',
);
-
Cheers!
Sushil H
seems broken in 5.3
not sure if this is known, but for some reason adding these to my template.php causes a javascript error when adding or editing a node:
drupal.js line 31
"button has no properties"
related to the Drupal.redirectFormButton() function
anyone have a workaround?
Watch out for IE6/IE7
This works quite well, but if you're relying on the button's 'op' value to drive conditional logic in the form's _submit function (for instance if you have multiple image buttons with different values), you will probably run up against an IE annoyance: IE doesn't pass values from image inputs! I spent several hours tearing my hair out yesterday wondering why my multistep form with its pretty image buttons wasn't working in IE6/7.
I wound up tearing out the custom theming code and implementing the graphic buttons as CSS background images as described in the comments at http://drupal.org/node/51526.
Drupal 6 still does this
Drupal seems to want all buttons to be "submit" type. Arg!
I wasted a lot of time as
I wasted a lot of time as well assuming Drupal is able to generate a normal button and wondering why it keeps following the action defined in the
-header.
Still not fixed in drupal 6.14!
a real shame for core developpers ! FAPI is supposed to be one of the main elements of drupal and it's still containing bugs from 4.7 !!!
Once again, i lost several hours to fix a core bug with a theme function ...
Yep. Just got me with drupal
Yep.
Just got me with drupal 6.10.
Indeed, both #type button and
Indeed, both #type button and submit will render a <input type="submit"> But there is also another difference! The description of #type buttons says:
Format an action button. When the button is pressed, the form will be submitted to Drupal, where it is validated and rebuilt. The submit handler is not invoked.
'#type' => 'button' will not
'#type' => 'button' will not invoke the submit handler but it will still invoke validation handlers. Why?
--
Caleb Thorne
Developer | Monarch Digital
https://www.calebthorne.com
Yep, vary bad things, i use
Yep, vary bad things, i use suffix
$form['gemini_file_role']['gemini_role'] = array(
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#options' => gemini_get_roles(),
'#suffix' => ""
An alternative way
I think that a good way of preventing your button to submit is to use the following. I don't know the inner workings of the DOM or the javascript (jquery) that I use so this is open for discussion.
I am in debt to this site:
http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/