Special Considerations for Views Exposed Filters

I've taken to building customized administrative interfaces for clients using Views that are restricted by user role and plugged into the administration menu with a normal menu item. My latest creation was on an educational site that uses the Quiz module to test users who listen to online lectures. There's a special user role that has the ability to create quizzes for lectures, and we needed a simple way for them to locate any quiz or question node without having administer nodes access (as required by the default Content administration page). Thus the Quizzes and Questions View was born!

I didn't expect any hiccups, as I'd just be making a quickie table View that included quiz nodes and question nodes. Since access to the View is restricted by user role, I didn't even have to bother filtering out unpublished content. As I was adding the Node: Type filter, I decided on the fly to expose the filter so the administrators could easily drill down to a specific question type if necessary. I then added the necessary fields and went to my preview, where I discovered that forum nodes were included in the results. Yikes!

My first thought was that I'd messed up the settings on the filter, so I reviewed. I had selected the filter to only show Multi-choice question, Matching, True/false question, and Quiz nodes. The operator was locked, selection was limited to a single option, and I'd even checked the box to limit the options to the node types I'd selected. However, it appears that I had a faulty understanding of the Optional checkbox that needed correction.

I've made exposed filters before, and I knew that if I marked a filter as Optional it would default to an <Any> option. Because I limited my options to the node types I had selected, I expected it to still filter my results against all the possible node type options and limit it further based on my selection in the exposed filter. However, as long as the <Any> option was selected, the View simply would not filter for node type at all. The simple solution was to add a second node type filter that was not exposed and limited the nodes to the same types that I made available in the exposed filter. This doubling up gave me the behavior I was expecting.

This same method should hold true for any other exposed filter where you want the user to select from a subset of all the available options. As always, if there's an easier way, post it up in the comments.

Topics: 

Comments

You're in fire lately Ryan. I'm really loving your latest series of blogs that talk about the numerous quirks in Drupal and friends.

lol - Yeah, it's been an unusual season of blogging productivity. New ideas just keep popping up! Hope they're helpful. Smile

Hm... Not sure what Earl thinks about this, but for me, this sounds like a bug Smile

I think I would expect the same. If I'd truly mean ANY, then I wouldn't pre-select any option.

I couldn't decide if it was expected behavior or not. I think it would be more intuitive for the View to restrict itself to only available options, as that's how it would work had I not made the filter exposed. Perhaps I should see what a quickie patch would look like and throw it in the queue... but that might just be giving myself too much credit in being able to grok the source.

That's an interesting solution. I had the exact same issue around the same time as you. In the end I simply switched off 'optional', but indeed I expected it to restrict itself to the selected node types as well.
In the end I feel like I'm better off without the < any > list. I think it would have confused my client a bit.

I'm still looking for the perfect solution for administration pages. I am wondering if there is a page or group that showcases the most user friendly back ends. Any ideas?

Cheers,
Danny

The simple solution was to add a second node type filter that was not exposed and limited the nodes to the same types that I made available in the exposed filter. This doubling up gave me the behavior I was expecting.

It's great that Views works in that way. It's not a bug or quirk, it's a cool feature that gives you extra flexibility in creating exposed filters.
One example:
Your site has ten nodetypes, two of them are editable for admins with the role "editor". You now wanna create a View that shows content of all nodetypes to those editors ( = all nodetypes are checked in the type-filter for the view) but also wanna give them the ability to only show them the nodetypes, they can review and edit ( = the two editable nodetypes are checked in exposed-filter).
No problem with the technique you mentioned above. But impossible if the View is using the same options as the exposed-filter.

Yeah, good point. It's probably enough that anyone struggling with the issue should be able to turn up this post via Google. Lol