Quick and Easy "Or Admin" Filter for Views
Prerequisites: Drupal 6, Views2, PHP
Recently, we had the need to create an Organic Groups view that filtered nodes based on group membership or, if the current user was admin, to show nodes from all groups. We needed something like the "Published Or Admin" node filter, only applied to Organic Groups membership.
My first instinct was, "No problem! Organic Groups considers users with the administer nodes permission to be members of all groups." Unfortunately, this membership test does not apply to Views filters.
My second instinct was, "That's OK, we'll just use the user:roles filter". Unfortunately, the user:roles filter acts on the node owner rather the current user.
Contribs to the Rescue
Fortunately, there is a simple way around this using the contribs Views Or and Views PHP Filter. As you are probably aware, views filters are normally combined with logical ANDs, meaning that a node is included only if it satisfies the requirements of all the included filters. Views Or allows us to combine arbitrary filters with logical ORs instead, giving us a lot more flexibility.
Views PHP Filter allows us to execute arbitrary PHP code and return a list of node IDs as either an inclusive or an exclusive filter. For example, if you define a PHP filter that returns the nids (1, 2, 3) and then add a node:published filter, you'd get any of nodes 1, 2, and 3 that are also published. Of course, that wouldn't be particularly useful; however, since we can execute arbitrary PHP we can write some code that does this:
if (the current user is admin) then return (a list of all node ids), otherwise return nothing.
Combine that with Views Or and things start to get interesting. One fortunate thing is that if you have a null clause (like our "return nothing" case), Views Or does not generate anything in the resulting query.
Combining Views PHP Filter and Views Or
With that combination we can effectively get this kind of filter logic:
If the current user is not admin: if (the current user is a member of the node's group) ← no OR clause since we returned nothing
If the current user is admin: if (the current user is a member of the node's group OR the nid is IN (set of all nids))
Now the problem with this approach is that it incurs an extra database call every time the page is viewed (to get a list of all nids), plus the set of all nids could potentially get really large. If you remember, though, we can configure our PHP filter to be exclusive instead of inclusive.
Using Negative Logic
Now, armed also with the knowledge that nids start at 1, we use negative logic for our PHP code:
if (the current user is admin) then return (nid 0)
or
global $user;
if (in_array('admin', $user->roles)) {
return array(0);
}
You see where I'm going with this. Combined with the exclusive PHP filter option, we're saying "nodes that do not have nid = 0", which is to say, all nodes in the system.

So we end up with the following filters (of course, substitute your own node type):

That's it. With just a few lines of code and a couple of modules, you've created an "Or Admin" filter.


