0

I am trying to implement an AJAX callback to change a drop down list options based on the values of another drop down.I have looked into the examples online and the examples in the examples module. I am trying to implement the example on one of the admin pages forms.

To simplify, I tried to achieve the following: Just to change the title of the second dropdown with a random number once the first dropdown has changed. Please note that I am using a field collection field.

function myaction_form_alter(&$form, &$form_state, $form_id) {

$form['field_programme_permission']['und']['0']['field_programme']['und']['#ajax']=array(
        'event' => 'change',
        'callback' => 'programmes_ajax_callback',
        'method' => 'replace',
        'wrapper' => 'countries_wrapper'
      );


$form['field_programme_permission']['und']['0']['field_countries']['und']['#title']=rand(1,100);
return $form;
}

function programmes_ajax_callback($form, $form_state) {    
 return $form['field_programme_permission']['und']['0']['field_countries'];
}

It is as if programmes_ajax_callback is not triggered at all. I see this Drupal AJAX please wait message but nothing actually happens. The weird thing, If I submit the form and it doesn't pass validation, I don't even see this Drupal AJAX "please wait" message. I have simplified the code as much as possible to try to pin point the problem, but it didn't work...

Any ideas?

perpetual_dream
  • 1,046
  • 5
  • 18
  • 51
  • Do you get an error? Try to error_log('something') in your callback to make sure that you don't even get there. – carmel Jun 03 '13 at 14:07
  • Try returning a simple string instead of the $form array. return '
    Test
    ' in the callback. BTW do you use debugger?
    – carmel Jun 03 '13 at 14:10
  • 1
    Does `countries_wrapper` actually exists on the page? Possible issue. – Sumoanand Jun 03 '13 at 18:08

1 Answers1

0

The problem was caused by the fact that countries_wrapper didn't actually exist as I was actually calling it countries-wrapper somewhere else.

Find below the code that I have actually used. This code covers a muti-value (up to 10) field collection at which one field (countries) in the field collection is dependent on the other (programme). Hopefully, it will prove to be useful for someone.

    function mymodule_form_alter(&$form, &$form_state, $form_id) {

          for($i=0;$i<10;$i++) {
          if(($form_id=='user_register_form') || ($form_id=='user_profile_form')) {

              if(isset($form_state['values']['field_programme_permission'][LANGUAGE_NONE][$i]['field_programme'][LANGUAGE_NONE][0]['tid'])) {
                $programme_selected= $form_state['values']['field_programme_permission'][LANGUAGE_NONE][$i]['field_programme'][LANGUAGE_NONE][0]['tid'];
              } else {
                $programme_selected=0;
              }

              if(isset($form_state['field']['field_programme_permission']['und']['entity'][$i]->field_programme['und'][0]['tid'])){
                 $programme_selected=$form_state['field']['field_programme_permission']['und']['entity'][$i]->field_programme['und'][0]['tid'];
              }

          $form['field_programme_permission'][LANGUAGE_NONE][$i]['field_programme'][LANGUAGE_NONE]['#ajax']=array(
            'event' => 'change',
            'callback' => '_programmes_ajax_callback',
            'method' => 'replace',
            'wrapper' => 'countries_wrapper'.$i
          );
          $form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#title']='Countries';

          $form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#prefix']='<div id="countries_wrapper'.$i.'">';
          $form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#suffix']='</div>';
          $form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#options']=_countries_ajax_callback($programme_selected);

        }
        } 
return $form;
}

function _programmes_ajax_callback($form, $form_state) {
//we first need to know the triggering element, to know the index of the countries field that we need to affect.
$index= $form_state['triggering_element']['#field_parents'][2];
return $form['field_programme_permission'][LANGUAGE_NONE][$index]['field_countries'];
}

function _countries_ajax_callback($selected) {

  $programme_value = $selected;
  $options=array();
  if(taxonomy_term_load($programme_value)){
  $programme_taxonomy=taxonomy_term_load($programme_value);
  if(isset($programme_taxonomy->field_countries[LANGUAGE_NONE])) {
  $countries=$programme_taxonomy->field_countries[LANGUAGE_NONE];
  foreach($countries as $country) {
    $country_tid = $country['tid'];
    $country_term = taxonomy_term_load($country_tid);
    $country_name = $country_term->name;
    $options[$country_tid]=$country_name;
  }
  }
  }
  return $options;
}
perpetual_dream
  • 1,046
  • 5
  • 18
  • 51