I just want to know how we can achieve two ajax callback on one form submit.Is it possible?and how?
2 Answers
No, it is not possible. Why would it be? Ajax callback can return a piece of HTML to replace your AJAX wrapper. If you will call two, which one would Drupal use? How should it decide? That's why #ajax['callback']
is defined as a name of a function, not an array of names. with one function you avoid problems like that.
If you need to call two function in response to AJAX event, then write a callback that will include the logic I asked about in first paragraph, and call functions you need from that callback.
You can't do that directly. Instead, use a function that calls the two other functions.
'#ajax' => array(
'callback' => 'my_callbacks_wrapper',
)
...
Then:
my_callbacks_wrapper($form, $form_state) {
$first_callback_processed_form = first_callback($form, $form_state);
$second_callback_processed_form = second_callback($first_callback_processed_form, $form_state);
return $second_callback_processed_form;
}
Make sure your callbacks don't interfere.
-
Have an upvote. Also useful to bare in mind that an Ajax callback returns an array: return array('#type' => 'ajax', '#commands' => $commands); It may be that it's simpler to call the first callback, then add some values to the #commands array rather than call the second callback. It might be duplicating functionality, but it might also be that the whole callback isn't required.– grizCommented Nov 3, 2014 at 8:29
-
Yes griz, you are right. The exmaple is very simple, but as you say it'd be better to merge the results of the two functions. But keep in mind that using two function is a hack.– sanzanteCommented Nov 3, 2014 at 16:46
-
@tunic --> Is it possible to have two wrapper elements? Commented Aug 13, 2015 at 7:27