如何根据特定字段的值显示和隐藏 Drupal 7 字段

发布于 2024-10-22 03:09:59 字数 752 浏览 2 评论 0 原文

我在这里想做的是控制一组字段在节点视图上的显示方式。我不想通过用户角色控制它们,因为这将是节点级别权限,并且我不希望这些权限影响具有相同内容类型的其他节点。

例如,假设我有三个不同的角色:ROLEA、ROLEB 和 ROLEC。每个角色都有自己的字段访问权限集。首次创建节点时,具有 ROLEA 角色的用户可以看到几个字段并可以编辑这些字段的值。当节点发布时,通过规则模块调用规则/操作,并将该节点中的状态字段设置为 STAGE1。在这个事件(创建一个新节点)之后,如果用户(ROLEA)转到她自己刚刚创建的那个节点,那些之前可编辑的字段现在应该是只读的。这意味着当规则模块时,将状态字段的值设置为STAGE1,当该节点想要被Drupal加载时,我们需要检查状态值,并在此基础上,如果是例如STAGE1,则修改中的其他一些字段该特定节点,只读或可编辑,或者在某些情况下对用户隐藏它们。所以我的猜测是创建一个模块,每次加载该类型的节点时,检查状态字段(这是我们在该节点类型中创建的字段)并根据该字段的值决定哪个节点应该显示或隐藏。这应该覆盖为该特定节点类型上的那些字段设置的权限。

我想做的是创建一种方法来根据该内容类型中特定字段的值来控制哪个字段将是只读/可编辑/隐藏,该值已由 Roules 模块根据不同的阶段进行设置由不同的用户组在该节点上工作。我没有使用有机组。我使用 Drupal 7 和规则模块以及其他几个与权限相关的模块。但我发现没有这样的方法来处理节点级别的字段可见性,与用户角色分开。

您认为还有其他方法可以达到相同的结果吗?如果您能给我有关如何创建这样一个模块的想法,我将不胜感激。

我真的很感谢所有的意见。

What I am trying to do here is controlling how a group of fields are showing up on node view. I don't want to control them by user role, as this is going to be a node level permission and I don't want these permissions affect other nodes with the same content type.

For example imagine I have three different roles: ROLEA, ROLEB and ROLEC. Each role has it's own permissions set for accessing to fields. When a node is created for the first time, a user with role of ROLEA, can see couple of fields and can edit the value of those fields. When node is published, a rule/action get called through Rules module, and will set a status field in that node to STAGE1. After this event (a new node created), if user (ROLEA) goes to that node that was just created by herself, those fields that were editable before, should be read-only now. This means when Rules module, set the value of the status field to STAGE1, when that node wants to get loaded by Drupal, we need to check the status value, and based on that if it was for example STAGE1, modify some other field in that particular node, read only or editable, or in some cases hide them from user. So my guess is to create a module that every time a node of that type, is loading, check the status field (which is a field that we have created in that node type) and based on the value of that field, decide which node should show up or hide. This should override the permission that have been set for those fields on that particular node type.

What I am trying to do is creating a method to control which field is going to be read only /editable / hidden based on the value of a specific field in that content type, which has been set by Roules module, based on different stages of working on that node by different group of users. I am not using Organic Group. I use Drupal 7 and Rules module and couple of other permission related modules. But I found that there is no such a way to handle field visibility in node level, separate from user roles.

Do you think there is any other way to achieve the same result? I appreciate if you could give me an idea about how to create such a module.

I really appreciate any and all input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仄言 2024-10-29 03:09:59

您可以使用以下命令创建模块并在节点表单上添加挂钩: hook_form_alterhook_form_FORM_ID_alter

使用这种钩子,您可以根据您想要的任何内容(用户角色、字段值...)轻松修改节点表单并隐藏或只读特定字段

You can create a module and add a hook on node form using : hook_form_alter or hook_form_FORM_ID_alter

Using this kind of hook, you can easily modify node form and hide or make read only specific fields, based on whatever you want (user role, field value...)

唠甜嗑 2024-10-29 03:09:59

还有 hook_node_view_alter( ) 钩子和 hook_entity_view_alter() 挂钩,允许您在渲染节点之前修改节点的渲染数组。在那里,您可以将字段数组设置为 '#access' = FALSE 以便隐藏它们,或 '#access' = TRUE 显示它们。

如果节点的另一个字段中的日期是过去的时间(它是“订阅过期”字段),我将使用它来隐藏某些字段。所以我不需要更改编辑表单,只需更改字段显示即可。

There are also the hook_node_view_alter() hook and hook_entity_view_alter() hooks that allow you to modify the render array for a node before it is rendered. There you can set field arrays to have '#access' = FALSE so they are hidden, or '#access' = TRUE to show them.

I'm using this to hide some fields if the date in another field of the node has a time in the past (it's a "subscription expires" field). So I didn't need to change the edit form, just the field display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文