When two buttons are added to a form, one vanilla HTML button and one image button, $form_state['triggering_element'] is always the image button regardless of which button is clicked. If both buttons are HTML buttons then the triggering element is set properly. If both buttons are image buttons, the last button in the form will be the triggering element. This is true when using both ajax and normal submit.
In order to demonstrate, I have created a content type called "test" and used the following code for the buttons and their ajax callbacks.
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_test_node_form_alter(&$form, &$form_state, $form_id) {
$form['html_button'] = array(
'#type' => 'button',
'#value' => 'HTML button',
'#ajax' => array(
'callback' => 'mymodule_button_html_callback',
'wrapper' => 'mymodule_replace',
),
);
$form['image_button'] = array(
'#type' => 'image_button',
'#value' => 'Image button',
'#src' => drupal_get_path('module', 'mymodule') . '/image.jpg',
'#ajax' => array(
'callback' => 'mymodule_button_image_callback',
'wrapper' => 'mymodule_replace',
),
);
$form['#prefix'] = '<div id="mymodule_replace">';
$form['#suffix'] = '</div>';
}
/**
* Callback function for the HTML button's ajax event.
*/
function mymodule_button_html_callback(&$form, &$form_state) {
drupal_set_message('HTML button');
return $form;
}
/**
* Callback function for the image button's ajax event.
*/
function mymodule_button_image_callback(&$form, &$form_state) {
drupal_set_message('Image button');
return $form;
}
Comments
Comment #1
interx commentedLooks similar to
http://drupal.org/node/1342066
Comment #2
wojtha commentedThe workaround is to get remove
#valueproperty of image_button. If it exist, there is a bug in the Drupal form processing, specifically in the _form_button_was_clicked() function: According to this function, every element with non-empty'#has_garbage_value'property (like the image_button) and with non-empty#valueis considered to be a "triggering element". If there are multiple elements of that kind in the form, the last one is picked up.I'm currently trying this fix:
So the current workaround is to not use the #value property. However this property is allowed in the FAPI docs and thus this is kind of a Drupal WTF - an unexpected behavior.
Comment #3
wojtha commentedFix for D7. I'll post D8 patch later. It seems that FAPI doesn't change in that case.
The first patch just extends the test and is expected to fail.
Comment #4
wojtha commentedTitle correction.
Comment #5
damien tournoud commentedThe code looks correct. Why do you except it to behave differently?
Comment #6
wojtha commentedPatch against D8. The first patch file just extends the test module and is expected to fail.
Comment #7
damien tournoud commentedSee
form_type_image_button_value(), that transform the (kind of browser-specific) submit (as far as I know you can find bothelement_nameandelement_name.x, element_name.y, the latter actually being the HTML5 pick). As far as I understand, this code is perfectly correct. If an image element has a value, it is considered to be submitted.Comment #8
wojtha commentedMaybe I've explained it wrong. The current - buggy - behavior of Drupal form processing is unexpected, because the "#value" is allowed property according to API, but the
_form_button_was_clickedfunction doesn't count with that...Comment #9
wojtha commentedOk, I see your point. You're right, but it is not very DX friendly as the rest two types of "buttons" have that property. It is confusing. It happend to me today and to many people before (according to this and some other issue queues), plus there is no single word about that behavior in the Forms API.
So at least this is a bug in the documention and (IMHO) DX issue.
Comment #10
David_Rothstein commentedThis is very similar to #873070: When an image button appears after another button in a form, the wrong triggering element and #submit handlers are detected, which is older. Both have patches (with similar code changes in them), although this one is farther along in some ways and the other is farther along in other ways.
Should we close this issue and consolidate all work over there?
Comment #11
damien tournoud commentedI agree that this is confusing. But the real problem is that we are using
#valueforsubmitandbuttonin the first place. We should really be#default_valueor similar in that case...One simple thing we could do (and backport) is to use
#valueas the value of thealt=""attribute for image buttons. That way the behavior of image buttons is the same as other types of buttons (aka.#valueis the textual representation of the button).Comment #22
amber himes matzIs this still a point of confusion? Should the issue be revived for Drupal 9?
Comment #23
amber himes matzThis might be a duplicate of #2546700: Add support for detecting the triggering element when buttons are changed client-side?
Comment #24
amber himes matzComment #27
smustgrave commentedThank you everyone for this.
Closing as outdated as there hasn't been a follow up for #22
If still a bug please reopen updating issue summary for D10
Thanks!
Comment #28
mlncn commentedThis is absolutely still a bug in Drupal 11.
Button Field module documents:
And where i'm coming from, i confidently told Gin in #3539394: Image buttons cannot be kept in Gin's sticky form actions that core's image button form element is a valid alternative to a standard submit input form element:
And i get it looking perfect and then discover that no matter which button i press, the image button (soft delete!) is the one that gets incorrectly identified as the trigger button.
Comment #29
mlncn commentedAnd it is not a duplicate with changing values client side; although that may be related, this is being caused with alteration in a Drupal form/widget alter.
Comment #31
mlncn commentedComment #32
mlncn commentedEssentially the fix by wojtha in this issue 13 years ago and richthegeek and cvangysel in #873070: When an image button appears after another button in a form, the wrong triggering element and #submit handlers are detected 14 years ago remains correct and needed.
When image buttons are clicked, browsers do not set NAME in the POST, instead setting NAME_x and NAME_y (with x and y the coordinates of the click). To get the clicked button we need to test for these parameters instead of the presence of value— that is, equally . (Damien's suggestion of using
#valuefor image buttons also does not work, whether the button is pressed or not, whether it has a value or not, the value gets set anyway, and set to "true", presumably because of how image buttons are treated by the browser— the only thing that is safe to check are if coordinates were set.)And yes (replying to 13 year old comments from another issue now, but the same feedback will come up) these changes make the alternative condition specific to image buttons, but i'm fairly certain that is because that is all there is— the only other element in core which uses
#has_garbage_valueis Radios, which aren't ever going to be triggering a form submission.So, yeah, image buttons are ridiculous and Drupal core could totally deprecate them and not support them since basically nobody has cared for a decade (especially if we finally do #1671190: Use <button /> form element type instead of <input type="submit" />), but as long as we have ImageButton extending Submit we need to support it properly as an alternative to Submit, that works alongside Submit buttons.
The merge request is ready and works, but leaving Needs work as it needs a test showing that this fails even when value is not set in the initial form element definition. As my use case is altering an existing button there is some possibility that something about that is adding 'value' back, but definitely unsetting value is not enough.