[英]Drupal7 - Filter view based on current user's roles?
Using Drupal7, Views3 and the Node Reference module, I'm using a view to populate a node reference field on a custom content type. 使用Drupal7,Views3和“节点引用”模块,我正在使用视图在自定义内容类型上填充节点引用字段。 (So it's a view of "Reference" display type.)
(因此,这是“参考”显示类型的视图。)
I want to filter this view to show (and allow users to select) only: 我想过滤此视图以仅显示(并允许用户选择):
Filters 1 and 2 are always mandatory; 过滤器1和2始终是必需的; then either filter 3 OR 4 must also be mandatory.
那么过滤器3或4也必须是必需的。 I know I can rearrange filters into filter groups to make this work.
我知道我可以将过滤器重新排列到过滤器组中以使其工作。 The problem is that I cannot find a way to build filter 4.
问题是我找不到构建过滤器4的方法。
For filter 3, I had to bring in a Relationship of type "Content: Author", which made a lot of new filters appear, including the "User: Current" that I used for filter 3 (node author == current user). 对于过滤器3,我必须引入“内容:作者”类型的“关系”,这使很多新过滤器出现,包括我用于过滤器3的“用户:当前”(节点作者==当前用户)。
For filter 4, I need to filter based on the role of the current user ( not author), and I cannot find how to do that. 对于过滤器4,我需要根据当前用户 ( 而非作者)的角色进行过滤,而我找不到执行该操作的方法。
In the filters list there's a new "User: Roles" filter available, but it only refers to the "Content: Author" relationship, so it only checks the node author's roles. 在过滤器列表中,有一个新的“用户:角色”过滤器可用,但它仅引用“内容:作者”关系,因此它仅检查节点作者的角色。 That's not what I need.
那不是我所需要的。
I'm guessing I have to add a new Relationship to bring in the current user data (something like "User: current"), and then filter on that data, but I cannot find that in the Add Relationship screen. 我猜我必须添加一个新的Relationship来引入当前的用户数据(类似“ User:current”),然后对该数据进行过滤,但是在Add Relationship屏幕中找不到。
Any idea how to do that? 任何想法如何做到这一点?
Thanks in advance. 提前致谢。
Update: 更新:
Note: when editing the node reference field (in my custom content type), I see there's a "views arguments" field that allows to pass arguments to the view. 注意:当编辑节点引用字段(在我的自定义内容类型中)时,我看到有一个“视图参数”字段,该字段允许将参数传递给视图。 It's not clear whether this field accepts tokens, but if so, maybe I could use that field to pass the current user ID to the View.
目前尚不清楚该字段是否接受令牌,但是如果是这样,也许我可以使用该字段将当前用户ID传递给View。 But then I don't know how to use that in Views...
但是我不知道如何在Views中使用它...
After posting the same question on drupal.stackexchange , I was suggested a solution using some PHP code in Views (contextual filter). 在drupal.stackexchange上发布了相同的问题后 ,建议我在Views(上下文过滤器)中使用一些PHP代码解决方案。 It's the best solution I've found yet, so unless someone can propose a better solution, I'll stick with this one.
这是我找到的最好的解决方案,因此,除非有人可以提出更好的解决方案,否则我会坚持下去。
Here is the solution: https://drupal.stackexchange.com/questions/38205/alter-field-settings-using-hook/38922#38922 这是解决方案: https : //drupal.stackexchange.com/questions/38205/alter-field-settings-using-hook/38922#38922
And here is the gist of it: 这是要点:
In the View, add a contextual filter on Author: Uid
, and use the following settings: 在视图中,在
Author: Uid
上添加上下文过滤器,并使用以下设置:
WHEN THE FILTER VALUE IS NOT AVAILABLE: 当过滤器值不可用时:
Use the following code: 使用以下代码:
global $user;
if (in_array('administrator', $user->roles))
{return -1;}
else
{return $user->uid;}
WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED 提供过滤器值或提供默认值时
Use the following code: 使用以下代码:
if ($argument == -1)
{return FALSE;}
else
{return TRUE;}
And that's it! 就是这样!
Notes: 笔记:
Compared to my initial settings, the relationship (Content: Author) isn't needed anymore; 与我的初始设置相比,不再需要这种关系(内容:作者); neither is the "Author" filter (which was brought in by the relationship anyway).
也不是“作者”过滤器(无论如何是由关系引入的)。
For Drupal 6, the condition in the first PHP snippet should rather be if (in_array('super user', array_values($user->roles)))
对于Drupal 6,第一个PHP代码段中的条件应该是
if (in_array('super user', array_values($user->roles)))
You can allow other roles as well, simply by editing the condition above. 您也可以通过编辑上述条件来允许其他角色。 For instance:
例如:
if (
in_array('administrator', $user->roles) ||
in_array('editor', $user->roles)
)
After adding the author relationship, there should be a new filter criteria User: Roles
. 添加作者关系之后,应该有一个新的过滤条件
User: Roles
。
Inside your views filter criteria, you will need to have 2 filter groups combined by OR ; 在您的视图过滤条件中,您需要将2个过滤器组按OR组合在一起; First group contain Filters 1, 2 and 3. And the other group should contain filters 1, 2 and 4.
第一组包含过滤器1、2和3,而另一组应包含过滤器1、2和4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.