0

I am giving the code of my main form and the controller of another module where the ajax callback is written. Actually this ajax callback function will be used in different forms. So I want to create a common module to call this function. Kindly help. This is the form field of main form:

    $form['construction_type'] = array(
  '#type' => 'radios',
  '#title' => $this->t('1. Whether Boiler or Economiser:'),
  '#default_value' => 'b',
  '#options' => array('b' => $this->t('Boiler'), 'e' => $this->t('Economizer')),
  '#ajax' => [
                  'callback' => '\Drupal\common_functions\Controller\CommonFunctionsController :: ajax_load_boiler_size',

               ],
);

And this is the ajax callback function in controller of another module

    class CommonFunctionsController extends ControllerBase{  public function ajax_load_boiler_size(array &$form, FormStateInterface $form_state){
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#boiler_size_div', $form['boiler_size']));
return $response;

}

The error message is Symfony\Component\HttpKernel\Exception\HttpException: The specified #ajax callback is empty or not callable.

4
  • These are two different Ajax implementations in Drupal, within Form API and outside of Form API by using a controller as custom endpoint. You can't mix both. No problem is to use a callback/controller from another module, in both cases. – 4k4 Mar 20 '20 at 8:52
  • Can you please explain what mistake I have done here? I am novice at Drupal 8 – Payel Ganguly Mar 20 '20 at 9:02
  • Put in #ajax a callback with $form and $form_state as argument. It doesn't matter where this callback is located, it can be a different module. I doubt you want to use a controller as custom endpoint, at least you would need to explain why you want to do this. – 4k4 Mar 20 '20 at 9:07
  • Actually this ajax callback will be used in several modules. So I want to create a common place where this callback will be written. What is the appropriate process to achieve this purpose? – Payel Ganguly Mar 20 '20 at 9:11
2

If anything, this should be:

'callback' => ['\Drupal\common_functions\Controller\CommonFunctionsController', 'ajax_load_boiler_size'],

That's only if it's going to work though. No guarantees.

1
  • Sorry for late reply. I am extremely thankful to you. It has worked. – Payel Ganguly Apr 27 '20 at 5:37

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.