简体   繁体   English

有关分类术语和用户角色的Drupal过滤器视图

[英]Drupal filter view on taxonomy term and user role

I have a rather basic question but I can not wrap my mind around how to solve it "nice". 我有一个相当基本的问题,但我不能全神贯注于如何“好”地解决它。 I have a node type that contains calendar info, I use views to get the month-view rendered nice and all is good so far. 我有一个包含日历信息的节点类型,我使用视图使月份视图呈现得很好,并且到目前为止一切都很好。 Next thing was to make the calendar view also contain nodes of type "group_xyz" but only display these events when a user is logged in I managed to do that with filtering using a couple of lines PHP to get user id and node type, but already here it didnt feel like a real drupalish solution... 接下来的事情是使日历视图也包含“ group_xyz”类型的节点,但仅在用户登录时才显示这些事件。我设法使用几行PH​​P进行过滤以获取用户ID和节点类型,但已经在这里,它并没有感觉像是真正的糟透解决方案...

To my problem, we would like to limit the display of calendar posts depending on the user role and a taxonomy term used in the node. 对于我的问题,我们想根据用户角色和节点中使用的分类术语来限制日历帖子的显示。

So the node has a taxonomy term reference "visibility_to" lets say one of (All, Internal, Administrators) set. 因此,该节点具有一个分类术语引用“ visibility_to”,可以说(全部,内部,管理员)集合之一。 The user can be either guest, logged in user or administrator. 该用户可以是访客,登录用户或管理员。

Now I would like to find a nice way to filter like this: Display the item IF node-type is calendar AND ( (visibility_to == "All") OR ( (visibility_to == "Internal") AND (role=="administrator" OR role == "logged_in") OR (visisbility_to == "Administrators" AND role =="administrator")) 现在,我想找到一种过滤这样的好方法:如果节点类型为日历,则显示项目AND((visibility_to ==“ All”)OR(((visibility_to ==“ Internal”))AND(role ==“ administrator “ OR角色==” logged_in“)或(visisbility_to ==”管理员“ AND角色==” administrator“))

I think you get the point... I think I could manage to hard code this in PHP but the day when we add new roles, new taxonomy terms I would rather have all this configured using the admin interface... 我想您明白了...我想我可以用PHP对此进行硬编码,但是当我们添加新角色,新分类法术语的那天,我宁愿使用管理界面来配置所有这些内容...

Any help and/or suggestions is appreciated. 任何帮助和/或建议,表示赞赏。 Also posted in drupal forums at: http://drupal.org/node/1879238 也发布在drupal论坛上, 网址为: http : //drupal.org/node/1879238

OK, here's how I'd do it in a 'Drupalish' way: 好的,这是我将以“ Drupalish”方式进行的操作:

  1. In your 'visibility' vocabulary, I'd add a field that is a role reference (in Drupal 7 terms can have their own fields -- yay!). 在您的“可见性”词汇表中,我将添加一个作为角色引用字段 (在Drupal中7个术语可以有自己的字段-是的!)。 You have to download and enable the Role Reference module to be able to set up this type of field. 您必须下载并启用“ 角色引用”模块才能设置这种类型的字段。 For my example, I will call the term's role-reference field 'Visibility role' 在我的示例中,我将该术语的角色参考字段称为“可见性角色”
  2. Now modify all your visibility terms and fill in the newly created role field for each, essentially setting which role is applicable to which term. 现在,修改所有可见性术语,并为每个可见性术语填写新创建的角色字段,从本质上设置哪个角色适用于哪个术语。 I suppose you could just have a role reference directly on the node, rather than behind the term, but that's up to you 我想您可能只是直接在节点上有一个角色引用,而不是在术语后面,但这取决于您
  3. In your view, add the relationship 'Content:visibility' or whatever that term is called 在您看来,添加关系“ Content:visibility”或该术语称为
  4. Finally, add a contextual filter that will compare the role in the 'visibility' term to the current user's role. 最后,添加一个上下文过滤器,该过滤器会将“可见性”一词中的角色与当前用户的角色进行比较。 Only after you've added the above relationship, will the field 'Visibility role' pop-up in the list of available contextual filters. 只有在添加了上述关系之后,才会在可用的上下文过滤器列表中弹出“可见性角色”字段。 Select that as your contextual filter. 选择它作为上下文过滤器。 Choose 'Provide default value' and 'PHP code' as your options. 选择“提供默认值”和“ PHP代码”作为选项。 Type in the following php code to compare the roles associated with the node's term with the role of the current user : 输入以下php代码,以将与节点术语相关联的角色与当前用户的角色进行比较

     global $user; $current_roles = ""; foreach ($user->roles as $key => $val) { $current_roles .= $key; if ($val != end($user->roles)){ // If not last item, add a '+' which treats these as an or $current_roles .= '+'; } } return($current_roles); 

* Finally, make sure you expand the 'More' option under the contextual filter and select to allow multiple values for this filter. *最后,请确保在上下文过滤器下展开“更多”选项,然后选择允许该过滤器使用多个值。

Try it out and let us know if this works for you!! 试试看,让我们知道这是否适合您! I tested it out and it seems to work for me. 我测试了一下,它似乎对我有用。

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

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