Drupal 7: how to filter view content (with entity

2019-08-14 09:19发布

问题:

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"