Elements with #has_garbage_value and #value are always set as a triggering element

Created on 24 February 2012, about 13 years ago
Updated 27 June 2023, almost 2 years ago

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.

<!--break-->
/**
 * 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;
}
πŸ› Bug report
Status

Closed: outdated

Version

9.5

Component
FormΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States BassistJimmyJam

Live updates comments and jobs are added and updated live.
  • DrupalWTF

    Worse Than Failure. Approximates the unpleasant remark made by Drupal developers when they first encounter a particular (mis)feature.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank 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!

Production build 0.71.5 2024