Drupal 7: how to filter view content (with entity reference field) based on current page content

被刻印的时光 ゝ 提交于 2019-12-11 10:22:44

问题


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

  • ContentA
  • ContentB (with a field Entity Reference to 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.

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

Could you help me?

Thanks


回答1:


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

Hope it helps

UPDATE

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"



来源:https://stackoverflow.com/questions/26422029/drupal-7-how-to-filter-view-content-with-entity-reference-field-based-on-curr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!