Drupal 8 Views: Ignore a contextual filter for admin role

1.4k Views Asked by At

I have a view serving an entity reference field. I need it to: (1) pull content authored by the currently logged in user, (2) unless the currently logged-in user has "administrator" role, in which case pull all content.

Currently I have the contextual filter set to "authored by," which works fine for requirement (1). I think I need it to bypass the filter based on role. For "block" or "page" displays, I can just duplicate a display, set permissions for it based on role, and it's seamless. But because this is an entity reference field, I can only specify one display in the field settings. Any ideas?

1

There are 1 best solutions below

0
On

In Drupal 8 I was able to do this without any contrib module like contextual filters OR.

  1. add the Authored By contextual filter for your entity type
  2. choose provide default value... from logged in user
  3. below in the "when value provided" section select "specify validation criteria
  4. For validator select User ID
  5. check "restrict user based on role"
  6. select your NON-ADMIN roles
  7. for "Action to take if filter value does not validate" select "display all results"

So if viewed by the non-admins is will show only where the logged in user id matches the entity owner. For admins it will show all.