繁体   English   中英

Drupal-节点形式的对象可见性(按角色)

[英]Drupal - Node-form objects visibility by role

我正在设定节点表单的主题。 我希望经过身份验证的用户拥有尽可能少的字段,而作为管理员,我希望查看所有字段。 我该如何编写php if语句来检查当前登录用户是否是管理员?

global $user;

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

在节点上时,还有一个$is_admin变量可用(不确定是否总是这样)。 有关用户的更多信息, $user数组将保存所有需要的信息

这里似乎有些模棱两可。 您可以使用主题模板中的以上代码来控制向最终用户显示字段。 它不会影响内容编辑或创建表单中字段的显示。 为此,您可能需要使用field_permissions(cck的一部分),它根据角色限制对字段的访问。

CCK内容权限。

控制用户通过主题层看到哪些字段是一种非标准的做法。 最好正确地使用访问控制系统,这样其他开发人员将知道如何针对自己的更改再次进行调整。

我将使用以下代码创建一个模块:

<?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结构中打印出完整的打印件,请粘贴drupal_set_message(print_r($form, TRUE)); 在hook_form_alter()实现中查看其中的内容。 更好的是,安装Devel ,然后通过插入dpm($form);可以获得更好的主题输出dpm($form);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM