0

I have a custom form with 2 dropdowns which trigger an ajax callback on change and change the html inside a wrapper. If I use a submit input with this form which has its own submit callback function, it works fine but if I change the input to image_button with same callback function, changing the values in dropdown ends up triggering image_button callback.

$form['fieldset1'] = array(
    '#type' => 'fieldset',
    '#title' => t( 'Fieldset Title' ),
    '#title_display' => 'invisible'
);
$form['fieldset1']['select1'] = array(
      '#type' => 'select',
      '#title' => t( 'Select 1' ),
      '#title_display' => 'invisible',
      '#options' => $options,
      '#default_value' => $default_value1,
      '#ajax' => array(
          'event' => 'change',
          'callback' => 'select1_callback',
          'wrapper' => 'my_wrapper_id',
          'method' => 'replace',
          'prevent' => 'click',
          'effect' => 'fade'
      )
  );
$form['fieldset1'] ['select1'] = array(
    '#type' => 'select',
    '#title' => t( 'Select 2' ),
    '#title_display' => 'invisible',
    '#options' => array_combine( range( 1, 10 ), range( 1, 10 ) ),
    '#default_value' => $default_value2,
    '#ajax' => array(
        'event' => 'change',
        'callback' => 'select2_callback',
        'wrapper' => 'my_wrapper_id',
        'method' => 'replace',
        'prevent' => 'click',
        'effect' => 'fade'
    )
);
$form['fieldset1']['to_be_changed'] = array(
    '#type' => 'item',
    '#prefix' => '<div id="my_wrapper_id" >',
    '#suffix' => '</div>',
    '#title' => $to_be_changed_value,
);
//This works.
$form['fieldset1']['submit'] = array(
    '#type' => 'submit',
    '#value' => t( 'Submit Button Value' ),
    '#submit' => array( 'button_callback_function' )
);
//This doesn't work. The button works but dropdown callbacks call button callback
$form['fieldset1']['image_button'] = array(
    '#type' => 'image_button',
    '#name' => 'image_button_name',
    '#value' => t( 'Image Button Value' ),
    '#src' => drupal_get_path( 'module', 'my_module' ) . '/assets/img/image.png',
    '#submit' => array( 'button_callback_function' )
);

Not sure what I'm missing over here.

  • Can you try to change the wrapper id in second field set? like 'my_wrapper_id' to 'my_wrapper_id2'. you have same wrapper id in those two fieldsets. – NTT Apr 5 '16 at 11:57
  • That's because the onchange event on both select fields is supposed to change html in that wrapper. Plus even if I remove the second select field completely, it doesn't solve the issue. – Drupal 8 test Apr 6 '16 at 1:52
0

Found the problem with the code. Image_button won't have any #value. It will only have return_value which in this case can be same as what I was putting in #value.

    $form['fieldset1']['image_button'] = array(
    '#type' => 'image_button',
    '#name' => 'image_button_name',
    '#return_value' => t( 'Image Button Value' ),
    '#src' => drupal_get_path( 'module', 'my_module' ) . '/assets/img/image.png',
    '#submit' => array( 'button_callback_function' )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.