0

I have develop many custom form and added ajax functionality with the form field, Ajax code works like a charm with my custom form.

But now i have a situation where i have add ajax callback to the title field of node.

For this i am using hook_form_alter, and adding same ajax code to title, but ajax event is not firing up for title field.

below is my code:

function module_form_FORM_ID_alter(&$form, FormStateInterface $form_state, $form_id){

  $form['title']['#ajax'] = [
    'callback' => 'module_updateCategoryDropDown',
    'event' => 'change',
    'progress' => [
      'type' => 'throbber',
      'message' => t('Verifying...'),
     ],
 ];
}

function module_updateCategoryDropDown(array &$form, FormStateInterface $form_state){
  /* form category dropdown code is here */
}
7
  • 1
    What are you expecting that code to do?
    – Clive
    Nov 25, 2017 at 13:05
  • I want to fire ajax event but when i type in title field no ajax event fired.
    – Udit Rawat
    Nov 25, 2017 at 13:33
  • 1
    But what are you expecting to change? Currently your code has nothing in to suggest you’re trying to update the page, so what method are you using to find out whether it works or not?
    – Clive
    Nov 25, 2017 at 15:11
  • 1
    You have to use the API for this as it happens, if you update the select options outside of the form build the submission will fail. Assuming you've replaced FORM_ID with the actual form ID, what you're missing is the wrapper in the AJAX array, and an html tag wrapping the select list with an ID that matches wrapper. That's what the form API uses to make the changes. Then you need to move the code to generate the select options on AJAX into the form alter. Base it on $form_state['values']['title']. Then do nothing in the AJAX callback except return that select element
    – Clive
    Nov 25, 2017 at 16:03
  • 1
    The examples module has an example pretty similar to what you're trying to do
    – Clive
    Nov 25, 2017 at 16:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.