Profile picture for user admin
Daniel Sipos
15 Nov 2013

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.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

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.

Contact us

Comments

Maxim 15 Nov 2013 09:27

The alternative is to use my

The alternative is to use my module
https://drupal.org/project/entityreference_filter
which shows selector filled by other view as the filter widget.

MrPhilbert 24 Nov 2013 23:14

In reply to by Maxim (not verified)

Documentation

Maxim, why would anyone use a module with virtually no documentation? Instead of posting about it here, how about spending at least a few minutes documenting your module?

no one in particular 15 Nov 2013 22:38

alternative

i tend to find it better to just add the relationship for the ER field, and access the desired fields (title, nid, whatever) to use as exposed filters that way. But this can be a useful quick tip when you know the list will be small.

luti 11 Apr 2015 18:15

Truly important hint

Daniel,
thanks a lot for this truly important hint! I spent quite a while trying to figure out was to do ... when I finally came across your post.
Cheers!

Jared 12 Jan 2018 10:41

Awesome hint

Thank you danny for this great hint. I was wondering how this can be achieved. Great Hint.

Add new comment