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

netrange.dev created an issue. See original summary.

yash_khandelwal’s picture

Try this example

<?php
function issues_menu() {
 $items['my/demo2'] = array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array('my_demo2'),
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,
    );
    return $items;
}

function my_demo2($form, &$form_state) {
    $dropdown_source = taxonomy_get_tree(3);
    $dropdown_array = array('0' => '--none--');
    foreach ($dropdown_source as $item) {
        $key = $item->tid;
        $term = taxonomy_term_load($key);
        $name = $term->name;
        $value = $item->name;
        $dropdown_array[$value] = $term->name;
    }
    $form['demo_form'] = array(
      '#type' => 'fieldset',
      '#prefix' => '<div id="demo_form_div">',
      '#suffix' => '</div>',
    );
    $form['demo_form']['state'] = array(
      '#type' => 'select',
      '#title' => t('Select State'),
      '#options' => $dropdown_array,
    );
    return $form;
}

function issues_form_my_demo2_alter(&$form, &$form_state, $form_id) {
    $dropdown_source = taxonomy_get_tree(3);
    $dropdown_array = array('0' => '--none--');
    foreach ($dropdown_source as $item) {
        $key = $item->tid;
        $term = taxonomy_term_load($key);
        $name = $term->name;
        $value = $item->name;
        $dropdown_array[$value] = $term->name;
    }
    $form['demo_form']['state'] = array(
      '#type' => 'select',
      '#title' => t('Select State'),
      '#options' => $dropdown_array,
      '#ajax' => array(
        'callback' => 'ajax_update_field_2_callback',
        'wrapper' => 'demo_form_div',
        'event' => 'change',
      ),);
    return $form;
}

function ajax_update_field_2_callback($form, &$form_state) {
    $form['demo_form']['city'] = array(
      '#type' => 'select',
      '#title' => 'Selected city',
      '#options' => array(
        'dubai' => t('Dubai'),
        'lundon' => t('Lundon'),
        'newyork' => t('Newyork'),
      ),
    );
    return $form['demo_form'];
}
netrange.dev’s picture

You 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.

apaderno’s picture

Priority: Major » Normal
Issue tags: -ajax form drupal hook