Search code examples
permissionsdrupal-7viewhook

Make menu tab on user profile visible only to profile owner


I made a "My bookmarks" tab on the user profile page using Views. The tab shows nodes the user has flagged.

However - "My bookmarks" should only be visible on the user's own profile page and at the moment the "My bookmarks" tab is visible on every profile a user visits. How do I check whether the current user matches the profile being viewed? I tried that from the View interface, but the access permissions don't have any options that work.

EDIT:

I think it is this code, but I still need some guidelines as to how to implement that:

<?php
global $user;
if (arg(0) == 'user' && $user->uid == arg(1)){
  return TRUE;
}
else {
  return FALSE;
}
?>

I also found this module, I think it helps a lot Views Access Callback


Solution

  • I managed to solve this using the code and module from above.

    The custom module contains this code

    <?php
    
     function MYMODULE_views_access_callbacks() {
      return array(
        'MYCALLBACK_user_has_access' => t('User can only see tab on his own profile'));
    }
    
    function MYCALLBACK_user_has_access() {
      global $user;
      if (arg(0) == 'user' && $user->uid == arg(1)){
        return TRUE;
      }
      else {
        return FALSE;
      }
    }
    
    ?>
    

    The Views Access Callback module adds your callback to the Views interface and from there, you can use it for your own view.