-1

i am fairly new to drupal , i have custom button added to one the content type edit page , when i click on that button i want to call a function but it seems like it is not calling function when i see the console i see drupa ajax error coming whenever i click the button . please let me know what i am doing wrong here .

function mymodule_form_node_program_edit_form_alter(&$form, FormStateInterface $form_state) {
  $form['apply'] = array(
    '#type' => 'submit',
    '#value' => t('Apply'),
    '#weight' => '9',
    '#executes_submit_callback' => FALSE,
    '#limit_validation_errors' => array(),
    '#ajax'                     => array(
      'callback' => 'button_test_callback',
    ),
  );
}
    function button_test_callback(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  echo '<script>alert("Welcome")</script>';
  return $form;
}

error i am getting :

Uncaught Drupal.AjaxError {message: "\nAn AJAX HTTP error occurred.\nHTTP Result Code: 50…\Core\DrupalKernel->handle(Object) (Line: 19)\n", name: "AjaxError"} message: "\nAn AJAX HTTP error occurred.\nHTTP Result Code: 500\nDebugging information follows.\nPath: /node/680911/edit?destination=/admin/content&ajax_form=1\nStatusText: error\nResponseText: The website encountered an unexpected error. Please try again later.Symfony\Component\HttpKernel\Exception\HttpException: The specified #ajax callback is empty or not callable. in Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse() (line 67 of core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php). Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object, 'kernel.exception', Object)\ncall_user_func(Array, Object, 'kernel.exception', Object) (Line: 111)\nDrupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.exception', Object) (Line: 227)\nSymfony\Component\HttpKernel\HttpKernel->handleException(Object, Object, 1) (Line: 79)\nSymfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 67)\nDrupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 57)\nDrupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)\nDrupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)\nDrupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)\nDrupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 49)\nAsm89\Stack\Cors->handle(Object, 1, 1) (Line: 50)\nDrupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 47)\nDrupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)\nDrupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)\nStack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)\nDrupal\Core\DrupalKernel->handle(Object) (Line: 19)\n"

1 Answer 1

0

You're using an D7 callback definition. It needs to be defined as a D8+ callback. So this:

function button_test_callback($form, &$form_state) {

Should be this:

function button_test_callback(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
16
  • i change to this still getting the same error , it seems the function button_test_callback is not getting called
    – vyshnavi
    Aug 24, 2021 at 18:13
  • I updated the code, it was missing a forwarrd slash, so you should try the updated code. Also, what file is button_test_callback() in?
    – Jaypan
    Aug 24, 2021 at 19:50
  • it is still not working , the button_test_callback is in the same file. i want change the value of node field when i click the custom button in the node edit form with out submitting the node edit form
    – vyshnavi
    Aug 24, 2021 at 21:02
  • "in the same file" <- What file is that?
    – Jaypan
    Aug 24, 2021 at 21:35
  • it is the custom module file , course_program.module
    – vyshnavi
    Aug 24, 2021 at 21:42

Your Answer

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

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