I've got a bunch of forms that are opening inside a modal so that the user is not redirected off the page. I've then got a few Ajax calls on form items inside the form. They all work when using the form in a page instead of a modal, however when in a modal it just gets an Ajax error.

The simplest Ajax functionality is the following:

function hook_form_alter(&$form, $form_state, $form_id) {
  $form['field_rate']['widget']['#ajax'] = array(
    'callback' => 'changeActualRate',
    'event' => 'change',
    'wrapper' => 'rates-wrapper'

function changeActualRate(array &$form, FormStateInterface $form_state) {
  // Get rate from taxonomy term
  $rate = $form_state->getValue('field_rate');
  if (!empty($rate)) {
    // Instantiate an AjaxResponse Object to return.
    $ajax_response = new AjaxResponse();

    $rate = taxonomy_term_load($rate[0]['target_id']);
    $cost = $rate->get('field_cost')->getValue();
    $cost = $cost[0]['value'];
    $ajax_response->addCommand(new InvokeCommand('#edit-field-actual-rate-0-value', 'val' , array($cost)));

    // Return the AjaxResponse Object.
    return $ajax_response;

It's a pretty simple bit of functionality but it seems no Ajax gets initialised from inside the modal. Anyone have any ideas on how to initialise Ajax once the modal is open. Modals are being opened via links using class="use-ajax" data-dialog-type="modal".

  • Try making the ajax handler static and changing the callback to '::changeActualRate'
    – user21641
    Oct 4, 2016 at 5:48
  • The functions are in the .module file so the code all works. The issue is Ajax not running from inside a modal. This includes inline entity forms and anything else that uses ajax calls. The loading icon appears in the modal as is expected but then the goes away with nothing changed.
    – Mark A
    Oct 5, 2016 at 0:31
  • I think the issue is that you have implemented AJAX wrong. See drupal.stackexchange.com/questions/188730/… for more info, especially on submit and ajax handlers and trigger_as.
    – user21641
    Oct 5, 2016 at 6:29
  • My Ajax is implemented correctly. I only need the "::" if it's inside a class. As this is running from a hook_form_alter in the .module file it just needs the function name. All my Ajax calls work perfectly when accessing the form from it's page. The issue comes when opening the form inside a modal, kinda like it's not being initialised when opening in the modal. I also have Ajax calls from contrib modules not working from inside the modal.
    – Mark A
    Oct 6, 2016 at 3:19
  • 3
    Possible duplicate of How to trigger Drupal.attachBehaviors after successful Ajax
    – kenorb
    Nov 13, 2017 at 16:27

1 Answer 1


It could be that your behaviors aren't processing the newly added modal elements because of an internal JS error.

I determined this because that was the problem discussed in this D7 answer:

How to trigger Drupal.attachBehaviors after successful Ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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