当我尝试hook_form_alter根据用户的角色使用隐藏字段时,我遇到了问题。我已经使用unset()并从$form数组中删除了该字段,但是在呈现表单时它仍然显示。
这是我的代码:
function mymodule_form_alter($form, $form_state, $form_id){
global $user;
if($form_id == 'my_content_type'){
if(array_key_exists(5,$user->roles) && !array_key_exists(3,$user->roles)){
if(empty($form_state['field']['args'][0]->title)){
unset($form['field_body']);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)