2010-07-04 64 views

回答

0
global $user; 

// Check to see if $user has the administrator role. 
if (in_array('administrator', array_values($user->roles))) { 
// Do something. 
} 

當節點上,也有一個$is_admin變量可用(如果不知道它在總是的情況下)。有關用戶的更多信息,$user陣列將容納所有需要的信息

+0

難道這是作爲一個片斷? – Toxid 2010-07-06 11:28:22

+0

嗯,只是嘗試;),它應該工作,但要記住在每個頁面上使用它很難維護,所以一個模塊/功能/ ..將是這樣一個更好的解決方案 – DrColossos 2010-07-06 11:42:25

0

這裏似乎有些不明確之處。您可以在主題模板中使用上述代碼來控制最終用戶的字段顯示。它不會影響內容編輯或創建表單中字段的顯示。爲此,您可能需要使用field_permissions(cck的一部分),它基於角色限制對字段的訪問。

0

CCK內容權限。

+0

我不認爲這個問題特定於CCK字段。 – Grayside 2010-07-06 16:37:30

0

控制用戶通過主題層看到哪些字段是非標準做法。最好是正確使用訪問控制系統,這樣其他開發人員將知道如何針對自己的更改再次調整事務。

我將創建與下面的代碼模塊:

<?php 
/** 
* Implementation of hook_form_alter(). 
*/ 
function custommodule_form_alter(&$form, &$form_state, $form_id) { 
    global $user; 

    // All node forms are built with the form_id "<machine_name>_node_form" 
    if (substr($form_id, -10) != '_node_form') { 
    // Only making changes on the node forms. 
    return; 
    } 

    // Make the menu field invisible to those without the administrator role. 
    // This will hide the menu field from users with the user permissions to make changes. 
    // Remember 'administrator' is not a default role in Drupal. It's one you create yourself or install a module (like Admin Role*) 
    $form['menu']['#access'] = in_array('administrator', array_values($user->roles)); 

    // This approach allows me to tie access to any permission I care to name. 
    // This specifically limits the menu field to menu administrators. 
    $form['menu']['#access'] = user_access('administer menu'); 
} 
?> 

使用這種方法,形式也根本無法建立這些元素當前用戶無法訪問。

如果您想了解節點表單頁面的表單元素,可以通過Google找到指導。如果您願意通過Form結構完整打印出來,請在您的hook_form_alter()實現中粘貼drupal_set_message(print_r($form, TRUE));以查看其中的內容。更好的是,安裝Devel,然後通過插入dpm($form);可以獲得更好的主題輸出。