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


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

我为Drupal编写了一个模块,我有一些字段(按钮、图像、文本)。

我希望一些字段可以为普通用户隐藏,并在管理员允许时为用户打开。

此功能使用"字段权限"模块实现。我不知道如何在我的模块中编写PHP代码,所以我可以使用"字段权限"模块更改权限吗?

在您的模块中,您可以根据以下角色设置这些字段的#访问权限:

$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;
}