Question

Using Drupal7, Views3 and the Node Reference module, I'm using a view to populate a node reference field on a custom content type. (So it's a view of "Reference" display type.)

I want to filter this view to show (and allow users to select) only:

  1. published content (OK)
  2. content of a certain type (OK)
  3. only nodes created by the current user (OK)
  4. OR if the current user is admin or some other role, bypass the previous filter (3) and show all nodes (but still respect filters 1 and 2) (not OK)

Filters 1 and 2 are always mandatory; then either filter 3 OR 4 must also be mandatory. I know I can rearrange filters into filter groups to make this work. The problem is that I cannot find a way to build filter 4.

For filter 3, I had to bring in a Relationship of type "Content: Author", which made a lot of new filters appear, including the "User: Current" that I used for filter 3 (node author == current user).

For filter 4, I need to filter based on the role of the current user (not author), and I cannot find how to do that.

In the filters list there's a new "User: Roles" filter available, but it only refers to the "Content: Author" relationship, so it only checks the node author's roles. That's not what I need.

I'm guessing I have to add a new Relationship to bring in the current user data (something like "User: current"), and then filter on that data, but I cannot find that in the Add Relationship screen.

Any idea how to do that?
Thanks in advance.

Update:
Note: when editing the node reference field (in my custom content type), I see there's a "views arguments" field that allows to pass arguments to the view. It's not clear whether this field accepts tokens, but if so, maybe I could use that field to pass the current user ID to the View. But then I don't know how to use that in Views...

Was it helpful?

Solution

After posting the same question on drupal.stackexchange, I was suggested a solution using some PHP code in Views (contextual filter). It's the best solution I've found yet, so unless someone can propose a better solution, I'll stick with this one.

Here is the solution: https://drupal.stackexchange.com/questions/38205/alter-field-settings-using-hook/38922#38922

And here is the gist of it:

In the View, add a contextual filter on Author: Uid, and use the following settings:

WHEN THE FILTER VALUE IS NOT AVAILABLE:

  • Provide default value
  • Type: PHP Code

Use the following code:

global $user;

if (in_array('administrator', $user->roles))
{return -1;}
else
{return $user->uid;}

WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED

  • Specify validation criteria
  • Validator: PHP Code

Use the following code:

if ($argument == -1) 
{return FALSE;}
else
{return TRUE;}
  • Action to take if filter value does not validate: Display all results for the specified field (this is what will give admins access to all results)

And that's it!

Notes:

Compared to my initial settings, the relationship (Content: Author) isn't needed anymore; neither is the "Author" filter (which was brought in by the relationship anyway).

For Drupal 6, the condition in the first PHP snippet should rather be if (in_array('super user', array_values($user->roles)))

You can allow other roles as well, simply by editing the condition above. For instance:

if (
    in_array('administrator', $user->roles) || 
    in_array('editor', $user->roles)
)

OTHER TIPS

After adding the author relationship, there should be a new filter criteria User: Roles.

Inside your views filter criteria, you will need to have 2 filter groups combined by OR; First group contain Filters 1, 2 and 3. And the other group should contain filters 1, 2 and 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top