2010-07-04 48 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);可以获得更好的主题输出。