I have created a view which displays a list of flagged content. I have used the Flag module to achieve this. I have named this page as Bookmarks, and the URL to this page is http://example.com/user/%/bookmarks. User should only able to access their own flagged content.

Consider this scenario:

  • User A has his flagged content in http://example.com/user/2/bookmark (uid=2 for User A).
  • This page should be accessible only by User A and the admin user.
  • If User B tries to access http://example.com/user/2/bookmark, it should return access denied.

How can I get this to work?

3 Answers 3


Install Views PHP and set access to PHP, then add this code there:

global $user;
if(($user->uid == arg(1)) || ($user->uid ==1))
return true
  • It works but admin user can access the page, but cannot see the flagged content
    – user50991
    Aug 25, 2015 at 12:52

ok i understand, you should add contextual filter like given below..enter image description here

i hope this helps you.

  • It should be access by own user. Other should not access it
    – user50991
    Aug 25, 2015 at 11:44
  • I have tried this. Still all user able to access it. Only admin and owner of the page must able to access
    – user50991
    Aug 25, 2015 at 12:29
  • ok now i understand better your problem is node view permission, so you can use hook_node_view api.drupal.org/api/drupal/modules!node!node.api.php/function/… then you can add permission baded on global user. Aug 25, 2015 at 12:35

To refine your view so that users can only access there own flagged content, perform these steps using the Views UI:

  • Add the relationship "Flags: Node flag", which is used to "Limit results to only those entities flagged by a certain flag; Or display information about the flag set on a entity.".
  • Next you need to "Configure" this relationship like so:
    • For "Flag" select the actual flag that you want this view to be about (e.g: select the "Bookmark" flag).
    • Make sure to set "By" = Current user.
  • If you completed the previous steps correctly, in the Views UI you should now have an extra relationship added with something like "Flags: bookmarks (by current user)".

With this, "user will only be able to access their own flagged content", since these are the only possible views results that will be shown. Note however that with only these refinements, you will not get an access denied (only an empty views result).


  • This will return the same results for the admin user (= only the results flagged by the admin user). If the admin user should be able to see flagged content of any user, I suggest you clone this view in another one, in which you omit this "Flags: Node flag" relationship (or simply keep the view you already have, and limit its access to admin users only).

  • With the above approach, the '%' in your URL has become obsolete.

Your Answer

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