0
votes

Here, I have alter the add content type form as follows:

function hc_listings_form_alter(&$form, &$form_state, $form_id)
{
//    print_r($form_id);
    if($form_id =='home_care_popular_search_region_node_form'){
        dpm($form,'form');
        $selected_states=hc_listings_load_agecare_regions();
//        dpm($selected_states,'selected');
        reset($selected_states);
        $first_state = key($selected_states);

//        dpm($first_state,'$first_state');



  $form['field_popular_state']=array(
        '#type' => 'select',
        '#default_value' => array_shift($selected_states),
        '#title' => t('Popular State'),
        '#description' => t('Home Care Popular State'),
        '#options'=>hc_listings_load_agecare_regions(),
        '#ajax'=>array(
            'callback'=> 'hc_popular_region_form_ajax',
            'wrapper'=> 'regions_list',
            'event'=>'change',
            'method'=>'replace',
        ),
        '#required' => TRUE,
  );
 $form['field_popular_region']=array(
     '#type' => 'container',
     '#tree'=>TRUE,
     '#prefix'=>'<div id="regions_list">',
     '#suffix'=>'</div>'

 );
 $form['field_popular_region']['list']=array(
    '#type' => 'checkboxes',
    '#title' => t('Popular Search Regions'),
    '#description' => t('Home Care Popular Search Regions'),
    '#options'=>get_popular_regions($first_state),
    '#required' => TRUE,

 );


  }
}

And the ajax Callback function is as follows:

function hc_popular_region_form_ajax($form, &$form_state){

  if(isset($form_state['values']['field_popular_state'])){
    $state=$form_state['values']['field_popular_state'];
    hc_my_log($state);

    $form['field_popular_region']['list']=array(
      '#type'=>'checkboxes',
      '#title'=>'Popular Search Regions',
      '#description'=>'Home Care Popular Search Regions',
      '#options'=>get_popular_regions($state)
    );

    hc_my_log($form['field_popular_region']['list']);

    $form['field_popular_region']['list']=form_process_checkboxes($form['field_popular_region']['list']);

 }

//    $form_state['rebuild'] = true;
    return $form['field_popular_region'];

}

This works but when the ajax callback is called it will execute the dpm($form,'form') (line:5) too. What is the reason for this?

And after that when I am submitting the form by clicking the default Save button in the node add form both popular regions and popular states values are not saved. What will be the cause of that? Should I have to alter the submission of the node too ?

1

1 Answers

0
votes

Here ,I have altered the form fields in a wrong way so in the form submission it alerts me with errors saying that the form structure has been changed in my altering.

The error is that it is not directly targetting the specific form field..so it should be corrected as follows:

 if($form_id =='home_care_popular_search_regions_node_form'){

        $selected_states=hc_get_states();
        reset($selected_states);
        $first_state = key($selected_states);
        dpm($first_state,'$first_state');
        $state= (isset($form_state['values']['field_popular_state']['und'][0]['tid']))?$form_state['values']['field_popular_state']['und'][0]['tid']:$first_state;

        //popular states field altering
        $form['field_popular_state']['und']['#default_value']=$selected_states[$first_state];
        $form['field_popular_state']['und']['#options']=$selected_states;
        $form['field_popular_state']['und']['#ajax']['callback']='hc_popular_region_form_ajax';
        $form['field_popular_state']['und']['#ajax']['wrapper']='regions_list';
        $form['field_popular_state']['und']['#ajax']['method']='replace';
        $form['field_popular_state']['und']['#ajax']['event']='change';

        //popular regions field altering
        $form['field_popular_region']['und']['#prefix']='<div id="regions_list">';
        $form['field_popular_region']['und']['#suffix']='</div>';
        $form['field_popular_region']['und']['#options']=hc_get_popular_regions($state);
}

Ajax callback is as follows:

function hc_popular_region_form_ajax($form, &$form_state){
  return $form['field_popular_region'];
}

Note: the method hc_listings_load_agecare_regions() has been changed to hc_get_states()