2

I have defined a textfield that includes an AJAX callback. In another separate file, I have the AJAX callback function defined.

Will this work or does the callback function need to appear in the same file the textfield was defined in?

This is the code I would use.

'associated_element' => array(
  '#type' => 'textfield',
  '#title' => t('Destination Data Element'),
  '#description' => t('Some text.'),
  '#ajax' => array(
      'event'=>'change',
      'wrapper' => 'linked_form_elements',
      'callback' => 'form_editor_linked_form_ajax',
    ),
  ),
);
  • 1
    "Will this work..." try it and report back with your results. – Deryck Jan 31 '14 at 0:39
  • The visible effect of having (any part of) your form definition being in an (extra) include file is: Your form event will trigger the AJAX post. (You can see the animation.) But the returned answer does not contain anything. (Nothing get's replaced.) Due to the #ajax=>callback function not found. – user18099 Jul 5 '17 at 7:18
2

use form_load_include() in your form definition function. I put this just above the return $form; line.

form_load_include($form_state, 'file_extension', 'your_module', 'your_file_name_without_extension');
0

It doesn't matter where your file locates, it depends on your module includes that file or not.. Your ajax callback is just a PHP function and it needs to be loaded before calling. You can define ajax callback in different file, but need to load that file before calling callback function. There are multiple ways that you can include files in Drupal, for ex, in .info file (if you want file be loaded globally), with module_load_include() function, or PHP's built-in require or include functions...

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.