Drupal 7:如何根据当前页面内容过滤视图内容(带有实体引用字段) [英] Drupal 7: how to filter view content (with entity reference field) based on current page content

查看:153
本文介绍了Drupal 7:如何根据当前页面内容过滤视图内容(带有实体引用字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的drupal 7中,我有2种内容类型,如下所示:

in my drupal 7 I have 2 content-types like these:

  • ContentA
  • ContentB(具有对ContentA的字段实体引用)

在ContentA的前端详细信息页面中,我想显示一个带有ContentB列表的块/视图,该列表的Entity Reference字段设置为当前ContentA.

In the front-end detail page of ContentA, I would love to show a block/view with a list of ContentB whose Entity Reference field is set to the current ContentA.

我查看了Block类型的视图并将其正确添加到页面中,但是我无法根据当前ContentA过滤ContentB.

I made a view of type Block and added it correctly to the page, but I cannot filter ContentB based on the current ContentA.

你能帮我吗?

谢谢

推荐答案

您应该为内容过滤器添加一个上下文过滤器,该值将用于过滤ContentB块. 然后,在上下文过滤器的当过滤器值不在URL中时"区域中,选择提供默认值",然后键入"PHP代码"(您应该为此启用php过滤器).在您的php代码区域中,您应该具有以下代码

You should add a contextual filter for the value you will use for filtering to the block View of ContentB. Then in your contextual filter in the "When the filter value is NOT in the URL" area select "Provide default value" and Type "PHP code" (You should have enable php filter for this). In your php code area you should have the following code

$node=menu_get_object();
 return $node->field_your_machine_field_name['und'][0]['target_id']; // this is the field you will use for fitlering

希望有帮助

更新

如果您需要在块中显示具有相同选择的相似结果(例如,在ContentA的引用字段中具有相同选择的ContentB的相似结果),则上述代码将起作用.我不会删除,因为您可能需要它也在您的项目中.我误会了.实际上,解决方案比较简单.您应该将上下文过滤器添加到该字段,然后在当过滤器值不在URL中时"区域中,选择提供默认值"和从URL提供ID"

The above code will work if you need to show in your block similar results with the same selection (for example similar results of ContentB with the same selection in the referencing field of ContentA ).I will not delete because you might need it also in your project.I misunderstood. Actually the solution is simpler. You should add the contextual filter to the field and in "When the filter value is NOT in the URL" area select "Provide default value" and "Provide id from url"

这篇关于Drupal 7:如何根据当前页面内容过滤视图内容(带有实体引用字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆