3

I have a view that uses a contextual filter and I want to limit access to that view based on the filter. To give it some context:

I have a content type that represents minutes from previous meetings. These minutes can belong to a handful of committees. The user selects which committee the minutes belong to when creating the node, and the view then uses that as a contextual filter.

I would like to know if there is something out there that would allow me to filter access to the view based on the contextual filter. So if someone tries to access minutes/mycommittee vs minutes/notmycommittee they would be allowed and denied respectively.

Thanks in advance.

4

If you are using Drupal 7 and Views 3.x, then for each contextual argument, you can specify a validation criteria from the Views UI. Look for this checkbox under the When the filter value IS in the URL or a default is provided section for a given contextual argument.

Suppose your committees are taxonomy terms in a vocabulary, say, Committee, then you can use the Taxonomy term validator. Otherwise, you can simply use the PHP Code validator and key in the custom logic that defines access to your view.

Think of this custom logic as a boolean method from which you can access the view object and the raw value of the argument and then based on this context, allow or deny the current user's access to your view accordingly.

| improve this answer | |
  • 1
    Thanks Amarnath. This is exactly what I was looking for. – Ryan Price Dec 18 '12 at 18:44
  • This also works in Drupal 8, however if your view is a local action/tab this doesn't control whether the view shows up in tabs. You need a views AccessPlugin for that. – Darvanen Apr 10 '18 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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