我正在主题化节点表单。我希望经过身份验证的用户拥有尽可能少的字段,而我作为管理员希望查看所有字段。如何编写用于检查当前登录用户是否为管理员的php if语句?
答案 0 :(得分: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
数组将包含所有需要的信息
答案 1 :(得分:0)
这里似乎有些含糊不清。您可以使用主题模板中的上述代码控制最终用户的字段显示。它不会影响内容编辑或创建表单中字段的显示。为此,您可能希望使用field_permissions(cck的一部分)来限制基于角色的字段访问。
答案 2 :(得分:0)
CCK内容权限。
答案 3 :(得分: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找到指南。如果您愿意在表单结构中填写完整的打印件,请在hook_form_alter()实现中粘贴drupal_set_message(print_r($form, TRUE));
以查看其中的内容。更好的是,安装Devel,然后插入dpm($form);
即可获得更好的主题输出。