我认为使用#disabled = true;是实现此目的的最简单方法,而不是在内核中添加一些繁重的模块,例如,当用户之前填充过user_profile_form时,我禁止对其自定义图像字段进行编辑。
function MY_CUSTOM_MODULE_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user_profile_form'){
if($form['field_national_front']['und'][0]['#default_value']['fid']){
$form['field_national_front']['und']['#disabled'] = true;
}
//ym($form);
}
}
另外,您可以检查用户的角色,仅对非管理员用户禁用(只读字段)。
function MY_CUSTOM_MODULE_form_alter(&$form, $form_state, $form_id){
global $user;
if($form_id == 'user_profile_form'){
if($form['field_national_front']['und'][0]['#default_value']['fid'] && !in_array('webadmin', $user->roles)){
$form['field_national_front']['und']['#disabled'] = true;
}
//ym($form);
}
}
field_permissions因此比以肮脏的方式进行工作要容易。