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



在这里,我更改了添加内容类型的形式,如下所示:

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值都不会保存。造成这种情况的原因是什么?我是否也应该更改节点的提交?

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

错误在于它没有直接针对特定的表单字段。。因此应按以下方式进行校正:

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()

最新更新