Have you ever wondered how you can use the Entity Reference (ER) field as a View filter (properly) in Drupal 7? In this article I am going to show you a little tip for turning the ER field into a nice select list as a filter in Views (either for the admin or exposed to users).
Let’s say you have 2 content types: Articles and Basic Pages. On the Article nodes you have an ER field that references to Basic Pages. If you create a View of Articles that you want to filter by I don’t know what Basic Page, you may find yourself adding the ER field as a filter only to find you need to provide a number as the Basic Page you want the Articles to be filtered by. Whaaat? Well yes, you need to provide the ID of the Basic Page referenced on the Article node.
If your View filters only from the admin and this is enough, that’s all good and well. But what if you want to expose this filter to your visitors and let them select the name of the Basic Pages by which the Articles should get filtered? Like with taxonomy terms for instance. The answer to this problem is hidden somewhere in the field attributes on the content type. All you have to do is tick a box and the ER field becomes much more useful in Views.
So let’s get it going. Edit the ER field on the content type it is used (in our case Article:
admin/structure/types/manage/article/fields/[your-field]) and all the way down under Additional Behaviours, tick the box
Render Views filters as select list. Save the field and go back to your View. Now if you choose the same field for the filter, you will get a select list of the node titles referenced with that field. And now you can also expose it to your visitors to use it for filtering the results of your View.
Please take note though of the message you get below that checkbox. If you have more that 100 entities referenced with that field, it’s not a good idea to use it like this. Nor I assume is a size you’d like a select list (either for the Views admin or for your users) to be.
Hope this helps.
Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.