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 */
}
FORM_ID
with the actual form ID, what you're missing is thewrapper
in the AJAX array, and an html tag wrapping the select list with an ID that matcheswrapper
. 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