2014-01-27 134 views
1

是否有任何设置基于用户角色的自定义表单(hook_form)字段的限制访问权限。 (即)字段权限模块为cck提供了这种灵活性,但不适用于自定义表单字段。Drupal自定义表单字段权限

回答

2

那么我不知道任何模块,但你可以做到这一点。

function custom_form(){ 

//obtained logged in user and his roles 
global $user; 
$current_role = $user->roles; 

//this form field is static 
$form = array(); 
$form['name'] = array(
    '#type' => 'textfield', 
    '#title' => t('name'), 
); 
//the below form fields are based on the current_role of the user 
if(in_array('test1', $current_role)){ 
    $form['conditional'] = array(
    '#type' => 'textfield', 
    '#title' => t('test1'), 
); 
} 
if(in_array('test2', $current_role)){ 
    $form['conditional'] = array(
    '#type' => 'textfield', 
    '#title' => t('test2'), 
); 
} 
return $form; 
} 

我不知道这是否是您需要的确切功能。 如果用户具有角色test1,则显示文本字段'test1',如果用户具有角色test2,则显示test2。

希望这会有所帮助。