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;
}
Comment | File | Size | Author |
---|---|---|---|
#6 | 1452894-5_trigerring_element_fix_test_fail.patch | 645 bytes | wojtha |
#6 | 1452894-5_trigerring_element_fix.patch | 1.35 KB | wojtha |
#3 | 1452894-3_trigerring_element_fix_test_fail.patch | 564 bytes | wojtha |
#3 | 1452894-3_trigerring_element_fix.patch | 1.25 KB | wojtha |
Comments
Comment #1
interX CreditAttribution: interX commentedLooks similar to
http://drupal.org/node/1342066
Comment #2
wojtha CreditAttribution: wojtha commentedThe workaround is to get remove
#value
property 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#value
is 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 CreditAttribution: 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 CreditAttribution: wojtha commentedTitle correction.
Comment #5
Damien Tournoud CreditAttribution: Damien Tournoud commentedThe code looks correct. Why do you except it to behave differently?
Comment #6
wojtha CreditAttribution: wojtha commentedPatch against D8. The first patch file just extends the test module and is expected to fail.
Comment #7
Damien Tournoud CreditAttribution: 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_name
andelement_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 CreditAttribution: 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_clicked
function doesn't count with that...Comment #9
wojtha CreditAttribution: 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 CreditAttribution: 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 CreditAttribution: Damien Tournoud commentedI agree that this is confusing. But the real problem is that we are using
#value
forsubmit
andbutton
in the first place. We should really be#default_value
or similar in that case...One simple thing we could do (and backport) is to use
#value
as the value of thealt=""
attribute for image buttons. That way the behavior of image buttons is the same as other types of buttons (aka.#value
is 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 CreditAttribution: smustgrave at Mobomo 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!