7

How can I hide a particular link in the main menu from anonymous users without making the whole menu invisible?

  • If that page is not visible to anonymous user then the link will be automatically removed. – j2r May 30 '12 at 16:24
  • And how can I configure who views the page? – Walker May 30 '12 at 16:40
  • 1
    What version of Drupal is this for? – Cravecode May 30 '12 at 17:22
  • I'm using Drupal 7. Version 7.14 to be precise. – Walker May 30 '12 at 17:56
5

You don't so much configure the visibility of links directly, but rather you configure the permissions of specific content types, and any link to content of a particular type will either be visible or invisible depending on the permissions of the content the link points to.

You can configure the permissions on any given piece of content by going into the Permissions section in Administration and finding the content type you want to restrict permissions on, and configure those permissions (check boxes for view, create, edit, etc..) based on role appropriately.

If you need to configure access to specific nodes/content, then you might have a look at Content Access.

  • That's the very nature of the problem. I need some pages to be visible to anonymous users, so I can't simply block them by content type. Isn't there a way to configure the menu block so it only shows certain links to people who are logged in? – Walker May 30 '12 at 17:59
  • Updated my answer with a link to a module that should be able to help you out. – Lester Peabody May 30 '12 at 18:05
  • @Lester I don't think this allows for access control at node level, I could be wrong but it seems one would need to create a specific content type for the nodes needing access control. But this is already built-in in Drupal, so I'm a bit confused as to what this module does? I will have to try this out. Thanks for sharing bro. – stefgosselin May 30 '12 at 20:45
  • If you click on the thumbnail for the project, there's a "per content node access settings" item that you can check off. It sounds like it adds another tab next to 'View', like 'Permissions' or something similar where you can configure perms just for that node. But yeah, I'd need to try it myself too before I could actually confirm it. – Lester Peabody May 30 '12 at 21:28
  • Sick, I'll have to tuck this one away in my little bag of tricks going forward. – Lester Peabody May 31 '12 at 15:08
2

You can do it manually. Maybe, it isn't the best form, but it may fix you problem temporarily.
You can add a JavaScript file to you main .tpl.php. In this file you can play with the parent and children elements of you code (http://api.jquery.com/parent), by JavaScript methods. So, first you have to get your element, by something like #idelement (it may be positioned in the menu code between all the leafs of the menu) and then hide the element by .hide(). Also, you have to add to your code that the JavaScript file added to the .tpl.php only if the user is logged in. Like:

if(user_is_logged_in()){
   drupal_add_js(yourjSFile);
}    

I repeat that is a temporary solution.
If you modified your code or element in the menu, this solution also must be modified.
This will only hide a menu item, and not the content types.

5

You can do this easily with a little bit of elbow grease!

There are two hooks for this depending on if your menu is generated by a module or by Drupal itself. In a custom module you would use either hook_menu or hook_menu_alter.

These two hooks have a parameter, access callback where you can give it a function that returns a BOOL. If the function returns TRUE, access is allowed and menu link is shown. If FALSE is returned, access is not allowed and menu link not displayed.

//Small example
function <your_module>_menu_alter(&$items) {
  // Example - disallow access for anonymous to node 15 from custom module
  $items['node/15']['access callback'] = '_accessCheck';
}

Then, further down you define the callback:

function _acccessCheck(){
  global $user;
  if ($user->uid) { // user is logged in
    return TRUE;
  } // Anonymous
    return FALSE;
}

Hope this helps, happy coding!

3

you could restrict users to see some of the menu links by using Menu per role module.
It allows you to restrict access to menu items based on user roles. So you could hide a particular menu link from a particular role and show it to others.

  • Does it hide the menu link or does it restrict access to the page it points to as well? – Lester Peabody Jun 1 '12 at 12:04
  • @LesterPeabody It hides the menu link. And hiding the menu link is as good as keeping the users to visit that page. I'm not sure if users might just guess the URL to visit the page ;-) – AjitS Jun 1 '12 at 13:24
  • I just have serious issues with that from a security standpoint. I don't understand the benefit to just hiding a link when they can still access the page it points to, just doesn't make any sense to me. – Lester Peabody Jun 1 '12 at 16:10
  • 1
    Hiding a menu item without restricting access has legitimate use cases, however hiding is no substitute for restricting if you need restricted content. That's bad. It's not just users guessing the URL (which by the way is an easy thing to do), it is also search engines indexing your pages if they are not restricted. If your content is sensitive this is very important. - That said, the question never stated that content access be restricted. – rooby Apr 23 '13 at 12:11
0

http://drupal.org/project/content_access works really well for me with Drupal 7.22.

It even hides the main menu links pointing to restricted content.

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.