1

Working on Drupal 8.9.1

I need to list nodes with Views and filter them based on user role shared by both nodes authors and current user.

If I add a relationship to content author and a contextual filter on (author)User ID with default provided by logged user ID, I only get nodes authored by the current user.

Let's say we have a role "Staff A", I need any user with this role to be able to see any nodes from any users with the same role "Staff A".

Thanks for any tip here.

2
  • Usually, the matching is done with the help of two Relationships, one from the content towards the user, then second one from the user backwards (reverse) to content or whichever you need. The second R using the first R then add contextual filter for user ID that is using the second R. But I don't see Roles available in Views Rs. Try drupal.org/project/views_published_or_roles In your case you might have more steps in between because you're matching roles and not directly user IDs content > author ID > author role > reverse role > user ID, then Contextual for UID using the last R.
    – prkos
    Commented Jun 30, 2020 at 23:20
  • Or use Roles Contextual filter with Provide default value option with database query where you figure it out for user roles.
    – prkos
    Commented Jun 30, 2020 at 23:22

1 Answer 1

0

Finally, using role as contextual filter was not a good choice. It is working now but with a different option.

Using a term reference field for both user and node with the same vocabulary was my solution.

In my example, let's say I have a taxonomy field called Staff (field_staff), related to a vocabulary called Staff too.

I have added this field to user account and content type (the one(s) I need in Views).

Then, within my View, I have added two relationships:

Content using field_staff and

User using field_staff with a relationship to the previous relationship (reverse__user__field_staff).

Finally, I have added a contextual filter on User ID provided by default (logged in user) and with a relationship to User using field_staff.

And voilà, the view is listing any nodes filtered by field_staff equal to the current user field_staff.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.