Hello everyone, just finished tinkering with a view of mine.

Let me outline the data model.

'job application' (content type) has a 'node reference' field linked to 'job posting',
'job posting' (content type) has a 'user reference' field (lets call it job staff) linked to ..users

so..

job application --> job posting --> job staff

I have my view set up to get job applications, showing job application and job posting details.
I have then made a block to display on the 'job posting' pages, using contextual filters to narrow results to only that job. Viewable by admin only.

I then wanted to make it viewable by the user associated to 'job posting',
added another contextual filter,
worked!!

But then because the admin user wasnt associated with the job posting, they couldnt see it.

I got round this by adding an exception for the admin user ID.
But this isnt very scaleable, what if i have multiple admin users.

What i want to do is be able to add a user_role to the exceptions,
or use a conditional contextual filter.. so if user_role isnt 'admin'.. run filter.
or at the least add multiple exclude values, but this means more work if more staff are added.

I think this may be possible with hook_views_query_alter(),
but i could do with some guidance on using this.

Thanks!!

Comments

aniket.mohite88’s picture

HI

got round this by adding an exception for the admin user ID.
But this isnt very scaleable, what if i have multiple admin users.

Were you able to find a solution.
I am so far able to gather the required ids (to be added as exceptions) in an array as well as comma separated values.
But, I am not able to add these to the exceptions part.

$users = entity_load('user');
$user_names = array(); //empty array
foreach ($users as $user) {
    //dpm($user);
    //Check if the current logged-in user has any of the below roles
    if(in_array('administrator', $user->roles) || in_array('Company Staff', $user->roles)) {
      $user_names[] = $user->uid;
    }
  }

$commaList = implode(', ', $user_names);
//$user_names: array of needed UIDs
//$commaList: Comma separated value of needed UIDs

Am I supposed to write this code in one of views hooks, because, I am not able to alter the output so far.

aniket.mohite88’s picture

I got this working with proper permissions & views itself.

Permissions (for a given content-type)
Administrator & Staff - Edit ANY content
Normal Member - Edit OWN content

Views
Contextual Filter - Content: Author UID

WHEN THE FILTER VALUE IS NOT AVAILABLE
- Provide default value: Type - "User ID from Logged in User"

WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED
- Specify validation criteria: Validator - "USER"
- Type of user filter value to allow - "Only allow numeric UIDs"
- Restrict user based on role: Restrict to the selected roles - SELECT the role who can only edit his own content
- Action to take if filter value does not validate - "Display All Results for the specified data" [important]

The last step (marked important), makes all the data (view results) available to other users, who have access to edit any content.

NOTE: Not sure, if this was your exact requirement, but this is one way to have that Contextual Filter multiple EXCLUDE or add have an contextual filter exclude user roles functionality.

Hope this helps

mikenewski’s picture

Thank you for your post, I'm trying to accomplish the same permissions as you have.
I'm using views-7.x-3.13 but I can't find the fields you are using, such as:
- Provide default value: Type - "User ID from Logged in User"
- Specify validation criteria: Validator - "USER"

Did you do this with Drupal 7?

aniket.mohite88’s picture

Yes. I did this in Drupal-7.

I'm using views-7.x-3.13 but I can't find the fields you are using, such as:
- Provide default value: Type - "User ID from Logged in User"
- Specify validation criteria: Validator - "USER"

This is the contextual filter (Author ID) that you add in the view.

Richard15’s picture

It works for admin, but after that the contextual filter stopped to work with normal user, until I remove it and ad it again...

Nganyu’s picture

i followed your steps carefully and it worked for me both on D7 and D8. Thanks

ericyellin’s picture

aniket.mohite88 thanks for the precise instructions.

davidredondo’s picture

Thank you very much, it worked perfectly