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

Created on 24 February 2012, over 13 years ago
Updated 27 June 2023, about 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 about 2 hours 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!

  • Status changed to Needs work 1 day ago
  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    This is absolutely still a bug in Drupal 11.

    Button Field module documents:

    Important note

    Due to #1452894: Elements with #has_garbage_value and #value are always set as a triggering element you can not use image buttons on edit forms when there are any other buttons. Non-editable displays are unaffected.

    And where i'm coming from, i confidently told Gin in πŸ› Image buttons cannot be kept in Gin's sticky form actions Active that core's image button form element is a valid alternative to a standard submit input form element:

    As noted in ✨ UX when using gin with Contnet Moderation and Workflows Active , Workflow Buttons module pairs very well with Gin for improving the experience of using content moderation workflows, and the 2.0.x branch of Workflow Buttons' "Trash workflow" submodule switches to using the image button to have a trash icon for the soft-delete, similar to the trash icon Claro/Gin add to core's delete link. […]

    But in short swapping the submit button out and substituting an image button for it works, and Gin should support this core form element when adding Gin attributes and looking for the #gin_action_item flag.

    And i get it working perfectly 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.

  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    And 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.

Production build 0.71.5 2024