简体   繁体   English

如何在Drupal中为按钮或字段设置自定义权限?

[英]How to set custom permission for buttons or fields in Drupal?

I wrote a module for Drupal and I have some fields (buttons, image, text). 我为Drupal编写了一个模块,并且有一些字段(按钮,图像,文本)。

I want that some fields could be hidden for normal users and open for users when admin let. 我希望某些字段对于普通用户可以隐藏,并在管理员允许时为用户打开。

This feature is implemented using "Field Permission" module. 使用“字段权限”模块可实现此功能。 I don't know how to write PHP code in my module, so can I change permissions using "Field Permission" module? 我不知道如何在模块中编写PHP代码,因此可以使用“字段权限”模块更改权限吗?

In your module you can set the #access of those fields depending on roles like: 在模块中,您可以根据以下角色设置这些字段的#access权限:

$roleId = The role id;
// If user doesn't have the role he can't access the field .
if (isset($user->roles[$roleId])) {
  // Deny access to the field.
  $form['FIELD_NAME']['#access'] = FALSE;
}

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

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