更改节点添加表单时不保存内容类型

时间:2017-11-09 04:24:56

标签: ajax forms drupal-7

在这里,我更改了添加内容类型表单,如下所示:

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,

 );


  }
}

ajax回调函数如下:

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'];

}

这有效,但是当调用ajax回调时,它也将执行dpm($form,'form')行:5 )。这是什么原因?

之后,当我通过点击节点添加表单中的默认“保存”按钮提交表单时,不会保存popular regionspopular states值。这是什么原因?我是否还必须改变节点的提交?

1 个答案:

答案 0 :(得分:0)

在这里,我以错误的方式更改了表单字段,因此在表单提交时它会提示我错误,表示我的更改中的表单结构已更改。

错误是它没有直接针对特定的表单字段。所以应该更正如下:

 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回调如下:

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

注意:方法hc_listings_load_agecare_regions()已更改为hc_get_states()