Score:-1

drupal custom callback in node edit form is not firing

eg flag

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"

Score:0
de flag

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) {
vyshnavi avatar
eg flag
i change to this still getting the same error , it seems the function button_test_callback is not getting called
Jaypan avatar
de flag
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?
vyshnavi avatar
eg flag
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
Jaypan avatar
de flag
"in the same file" <- What file is that?
vyshnavi avatar
eg flag
it is the custom module file , course_program.module
Jaypan avatar
de flag
Ok, that should work then. Please update the original post to show your current code.
vyshnavi avatar
eg flag
i updated the code , i am getting Uncaught Drupal.AjaxError as i mentioned in the question whenever i am clicking the button .
Jaypan avatar
de flag
If that's your current code, you need to return a value from the ajax callback.
vyshnavi avatar
eg flag
can you give an example ?
Jaypan avatar
de flag
To test, add `return $form;` to your ajax callback.
vyshnavi avatar
eg flag
i added the return $form and i updated the code in the question too , but still not firing the function i am still getting the ajax error in console
Jaypan avatar
de flag
That `echo` call may be causing you issues. Did you try without that?
vyshnavi avatar
eg flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/128913/discussion-between-vyshnavi-and-jaypan).
vyshnavi avatar
eg flag
hii jaypan , after reomving echo the error is not popping up but i want to change the field value $form['field_text_plain']['widget'][0]['value']['#value'] = '75433g'; but it not showing do i need to send the value in return ?
Jaypan avatar
de flag
Welcome to Drupal Answers - it's a one-question one-answer format site. The question of your empty callback has been solved, and therefore question you just asked is a new question. Please mark this question as solved, and then open a new topic for that issue.
vyshnavi avatar
eg flag
ohh sure , thank you for your help :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.