简体   繁体   English

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

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

I'm in the process of theming a node form. 我正在设定节点表单的主题。 I want authenticated users to have as few fields as possible, while I as an administrator want to see all fields. 我希望经过身份验证的用户拥有尽可能少的字段,而作为管理员,我希望查看所有字段。 How do I write a php if statement that checks if the current logged in user is an administrator? 我该如何编写php if语句来检查当前登录用户是否是管理员?

global $user;

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

When on nodes, there is also a $is_admin variable available (not sure if it's in always the case). 在节点上时,还有一个$is_admin变量可用(不确定是否总是这样)。 For more infos on the user, the $user array will hold all needed infos 有关用户的更多信息, $user数组将保存所有需要的信息

There seems to be some ambiguity here. 这里似乎有些模棱两可。 You can control the display of fields to end users using the above code in a theme template. 您可以使用主题模板中的以上代码来控制向最终用户显示字段。 It will not affect the display of fields in the content edit or create forms. 它不会影响内容编辑或创建表单中字段的显示。 To do that, you'll likely want to use field_permissions (part of cck) which limits access to fields based on role. 为此,您可能需要使用field_permissions(cck的一部分),它根据角色限制对字段的访问。

CCK内容权限。

It is non-standard practice to control which fields a user sees via the theming layer. 控制用户通过主题层看到哪些字段是一种非标准的做法。 It's better to use the access control system properly, that way other developers will know how to tweak things back again for their own changes. 最好正确地使用访问控制系统,这样其他开发人员将知道如何针对自己的更改再次进行调整。

I would create a module with the following code: 我将使用以下代码创建一个模块:

<?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');
}
?>

Using this approach, the form will simply not build those elements that the current user cannot access. 使用这种方法,表单将不会简单地构建当前用户无法访问的那些元素。

If you want to learn about the form elements of your node form page, you might find a guide via Google. 如果您想了解节点表单页面的表单元素,可以通过Google找到指南。 If you are willing to slog through a full print out of the Form structure, stick drupal_set_message(print_r($form, TRUE)); 如果您愿意从Form结构中打印出完整的打印件,请粘贴drupal_set_message(print_r($form, TRUE)); in your hook_form_alter() implementation to see what's there. 在hook_form_alter()实现中查看其中的内容。 Even better, install the Devel , and then you can get a much nicer themed output by inserting dpm($form); 更好的是,安装Devel ,然后通过插入dpm($form);可以获得更好的主题输出dpm($form); .

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

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