Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I am trying to add ajax callbacks to the taxonomy edit form by using the hook
MY_MODULE_form_taxonomy_form_term_alter.
I am trying to add an ajax callback to one of the form elements in my taxonomy for a select field as shown in the code below.
The problem is when I click on the first select field, nothing happens and no ajax call is made to Drupal.
function MY_MODULE_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
$form['#after_build'][] = '_MY_MODULE_form_taxonomy_form_term_after_build';
}
function _MY_MODULE_form_taxonomy_form_term_after_build($form, &$form_state) {
// this is a select field which I am trying to extend it with ajax
$form['MY_FIELD_1']['#ajax'] = array(
'callback' => 'ajax_update_field_2_callback',
'wrapper' => 'my-field-2-dropdown-second-replace',
'event' => 'change',
);
return $form;
}
// adding prefix and suffix to my second form element that exists in the $form already
$form['MY_FIELD_2']['#prefix'] = '<div id="my-field-2-dropdown-second-replace">';
$form['MY_FIELD_2']['#suffix'] = '</div>';
}
function ajax_update_field_2_callback($form, $form_state){
// Do some work here.
return $form;
}
Comments
Comment #2
yash_khandelwalTry this example
Comment #3
netrange.dev CreditAttribution: netrange.dev commentedYou example will pretty sure work, but here I am trying to alter a form that comes from Drupal Core, in particular, the taxonomy edit form. Trying to add ajax to that does not work.
Comment #4
apaderno