Community

Dependent select issue when editing the form using hook_form_alter()

Hi,

I'm altering a form created by D7's inbuilt cck. Here is the code of my hook_form_alter().

<?php
$options_first
= $form['field_business_sector']['und']['#options'];

        if (!isset(
$form_state['node']->field_business_sector)) {
                       
           
$sector_selected = isset($form_state['values']['field_business_sector']['und'][0]['value']) ? $form_state['values']['field_business_sector']['und'][0]['value'] : key($options_first);           
           
$domain_selected = isset($form_state['values']['field_domain']['und'][0]['value']) ? $form_state['values']['field_domain']['und'][0]['value'] : '';
           
        } else {
       
           
$sector_selected = isset($form_state['node']->field_business_sector['und'][0]['value']) ? $form_state['node']->field_business_sector['und'][0]['value'] : key($options_first);
           
$domain_selected = isset($form_state['node']->field_domain['und'][0]['value']) ? $form_state['node']->field_domain['und'][0]['value'] : key($options_first);

           
$sector_changed = isset($form_state['values']['field_business_sector']['und'][0]['value']) ? $form_state['values']['field_business_sector']['und'][0]['value'] : key($options_first);
        }
       
       
//print $sector_changed." + ".$sector_selected;
       
       
$form['field_business_sector']['und']['#default_value'] = $sector_selected;
       
       
// Bind an ajax callback to the change event (which is the default for the
        // select form type) of the first dropdown. It will replace the second
        // dropdown when rebuilt
       
$form['field_business_sector']['und']['#ajax'] = array(
         
// When 'event' occurs, Drupal will perform an ajax request in the
          // background. Usually the default value is sufficient (eg. change for
          // select elements), but valid values include any jQuery event,
          // most notably 'mousedown', 'blur', and 'submit'.
          // 'event' => 'change',
         
'callback' => 'ajax_dependent_dropdown_callback',
         
'wrapper' => 'domain-replace',
        );

       
$form['field_domain']['und']['#prefix'] = '<div id="domain-replace">';
       
$form['field_domain']['und']['#suffix'] = '</div>';
       
$form['field_domain']['und']['#options'] = _ajax_get_domain_dropdown_options($sector_selected);           
       
$form['field_domain']['und']['#default_value'] = $domain_selected;
?>

When I'm in the insert new record mode, things are perfectly working. When I'm in edit mode, the parent select field that is "field_business_sector" is showing the previously saved value and the child field "field_domain" is storing only related options and by default selecting previously saved value. This this point there is no problem.

Look at the 2 variables $sector_changed and $sector_selected. $sector_selected stores the saved value for "field_business_sector". And $sector_changed stores the changed value when changed through dorp-down list.

So, when I'm changing "field_business_sector" value, as $sector_selected variable has old saved value in it, ajax is fetching and populating same old list. If I replace the variable from $sector_selected to $sector_changed in the following line -

<?php
$form
['field_domain']['und']['#options'] = _ajax_get_domain_dropdown_options($sector_selected);
?>

it will work. But in that case the initial selection after the refresh on edit page will not be able to fetch related list as that variable is null initally.

I'm pretty new to drupal, can anybody suggest a work around or any smarter way.

Thanks

Comments

I need to know the standard

I need to know the standard way of doing dependent select on D7's CCK fields. The above is working but not populating dependent list at the time of form editing with on change event. Can somebody help?