我正在主題化一個節點窗體。我希望通過身份驗證的用戶擁有儘可能少的字段,而我作爲管理員希望查看所有字段。如何編寫一個php if語句來檢查當前登錄用戶是否是管理員?Drupal - 按角色顯示節點窗體對象的可見性
0
A
回答
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
這裏似乎有些不明確之處。您可以在主題模板中使用上述代碼來控制最終用戶的字段顯示。它不會影響內容編輯或創建表單中字段的顯示。爲此,您可能需要使用field_permissions(cck的一部分),它基於角色限制對字段的訪問。
0
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);
可以獲得更好的主題輸出。
相關問題
- 1. 窗體不顯示爲Drupal 7中的節點/內容類型
- 2. 在COM可見DLL中顯示窗體
- 3. 按角色可訪問的Drupal部分
- 4. Drupal - 燈箱 - > iframe節點顯示整個網站的意見
- 5. Drupal的8 - 管理 - 顯示該角色
- 6. Drupal - 塊可見性
- 7. ASP.NET對象的可見性
- 8. Android對象的可見性
- 9. Drupal - 顯示塊內節點的標題
- 10. 切換窗體元素的可見性
- 11. Drupal顯示節點註釋的節點的視圖
- 12. Drupal:搜索評論節點,並顯示父節點的結果?
- 13. 用Drupal 6顯示節點內的節點
- 14. Drupal 6:排序顯示節點引用
- 15. Drupal節點在存在時顯示404
- 16. Windows窗體可見性問題
- 17. Array對Drupal的Web窗體
- 18. Visual Studio窗體設計器和不可見對象的大小
- 19. Drupal的 - 展示節點
- 20. Drupal中的主體節點體6
- 21. 角色對象數組字節數組
- 22. 顯示對角按鈕
- 23. Drupal 7顯示塊中當前顯示的節點字段
- 24. Drupal的意見:允許用戶選擇節點顯示的排序標準
- 25. Drupal位置gmap節點映射不顯示在節點頁面
- 26. CSS屬性:顯示與可見性
- 27. 針對特定的WordPress角色顯示特定媒體
- 28. Drupal - 按角色檢查所有站點路徑的安全
- 29. Drupal的 - 在新窗口中打開節點體
- 30. 如何在drupal中爲單個節點顯示2種顯示?
難道這是作爲一個片斷? – Toxid 2010-07-06 11:28:22
嗯,只是嘗試;),它應該工作,但要記住在每個頁面上使用它很難維護,所以一個模塊/功能/ ..將是這樣一個更好的解決方案 – DrColossos 2010-07-06 11:42:25