1

I want to add an ajax callback to a form element inside a form_alter, but it already has 2 callbacks assigned to it.

When I review:

$form['payment_information']['payment_method']['#ajax']['callback']

it has 2 callbacks already assigned to it.

If I do this:

$form['payment_information']['payment_method']['#ajax'] = [
  'callback' => '_sia_general_get_fee',
  'event' => 'click',
];

My callback is hit on click; but of course, the other 2 callbacks are wiped out and do not work.

If I add something like this:

$form['payment_information']['payment_method']['#ajax']['callback'][] = '_sia_general_get_fee';

then the other callbacks are called; but mine is not - I suspect since I no longer of an event defined.

I am also not sure how the other ajax calls work when they have no events defined for them.

Is there any way to do this?

1

I doubt that the array you've found really assigns 2 callbacks. It's rather an array defining a class and a method for a single callback:

'callback' => ['Drupal\foo\BarClass', 'ajaxCallback']

In fact, you can only set one callback in an #ajax element. If you want to override a callback you have to include the overridden callback in your code:

Drupal\foo\BarClass::ajaxCallback($form, $form_state)

and add your ajax commands to the returned ajax response (or in case the original method returns a render array you can either add your own content to the array or transform the render array into an ajax command which you return with your own commands in an ajax response).

Caveat: This only works if the overridden callback is static. If it runs in an instantiated object you have to override the class method itself.

1
  • Thanks @4k4, that is what it looks like is there. I'll try the include in my callback idea.
    – liquidcms
    Jan 12 at 1:56

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.