The question has been asked a few times before, but no one ever seemed to get an answer about how to do it, or they would get an answer about how to show/hide an entire block. I just want to be able to show/hide a menu item. It seems like an obvious thing to do, so maybe I'm misunderstanding something about a "drupal way" to set this up.

I know that one of the most obvious ways is to check "permissions" but that doesn't work with all of my menu links, and it's not what I'm asking to do. I want to be able to restrict individual nodes so that only certain roles can see them.

For example, I want to put a hyperlink to a particular document (okay, node) on the naviagation menu, but I only want Anonymous users to see it. A node is not going to show up in the admin>access control>permissions area, so what can I do to make it show, or hide it from other roles?

Thanks,

Vince

Comments

styro’s picture

eg simple access, taxonomy access, node privacy by role etc?

Vanilla Drupal has back end support for node specific access rights, but no management interface built in. You need to install one of the access control modules to provide that interface - they all work quite differently, and which one you use will depend on how you want it to work.

Do all your testing of these modules on a test server though - they can/will conflict with each other.

I haven't explicitly tested it but I'm pretty sure menus would obey node access permissions.

--
Anton

VinceDee’s picture

I installed Simple Access, it works perfectly, and is almost exactly what I was looking for.

The only other thing I wanted to be able to do was make it so that certain roles would be able to see certain menu links to certain nodes while other roles wouldn't.

But I'm happy to have gotten this far, this was huge for me....Thanks!

Vince

---------
The hottest Linux distribution? Ubuntu
Talk about Ubuntu Web Servers at:
http://www.ubuntuwebservers.com

magnusprime’s picture

I have a similar need. I want to be able to edit the menu in a way that certain items in the menu show up to "registered" users while others show up to "anonymous" users.

Basically, I dont want Anonymous users being able to link to the forum or image gallery, but I want both on the menu, and only to show up when a user is registered.

Would the Simple Access mod work for me, or would I need something more?

agentrickard’s picture

The 'easy' way to do tbis is to set up a separate menu with items for anonymous users. That menu will be its own bloick, Then show/hide tbat block based on roles.

(Since this can be done through site admin without any code, I call it easy. That's why the first poster always got this type of answer from people.)

If your site doesn't change much, you can also turn off the default menus and rewrite them as custom PHP blocks (or template code). That will give you absolute control over who sees what. (I use this trick to create user dashboards that include the user picture.)

If Simple Access does this, I'd like to know, since I've never tested it.

--
http://ken.blufftontoday.com/
Search first, ask good questions later.

magnusprime’s picture

I'll give this method a try. Seems the easiest to me. But will the new block I create show the anonymous users the login screen? Or is that a special node, that I need to link to?

PakWaan’s picture

See this thread:

http://drupal.org/node/27690

No one will ever see the block (or the menu items in it) unless they are a member of the role you select. It works great on my site, combined with the taxonomy access module - I have set up a private area for a group of clients and no one else can see any of the content in that area. The taxonomy access module (or one of the other node privacy modules) is necessary because even if you don't provide a menu item for people outside the group, the "search" function may still reveal the nodes or pages in that special area and allow them full access through the back door.

VinceDee’s picture

What I had been originally looking for was a way to allow hyperlinks to be visible, but non-registered users would not be able to use the links (and it would send them to a registration page, of course).

Here are a couple of examples of what I wanted to do:

1. Anonymous users will see a "Why Register?" menu item on main menu, while registered users wouldn't

2. Anonymous users can view the Downloads page to see what's on there, but the hyperlinks to the files themselves don't work for them and, once again, send them to the registration page (seeing a pattern here?)

Anyway, that's basically why I'd like to be able to have access role control over the hyperlinks

---------
The hottest Linux distribution? Ubuntu
Talk about Ubuntu Web Servers at:
http://www.ubuntuwebservers.com

PakWaan’s picture

The "Why Register" part is easy - just make a block which contains the "Why Register" link, and only show it to unregistered visitors. It could contain the entire navigation menu for unregostered users, for that matter. Another block would contain the navigation items for registered users.

The downloads... use the same idea as above, except that the "Downloads" link on the unregistered users' block leads to a static page showing the downloads but links to your registration screen. It requires keeping 2 sets of each menu, but most navigation menus don't have more than a dozen things on them anyway.

If you want to do it using just links from the main navigation block, I don't know of a way to do it, but since it's Drupal, there's probably a module somewhere that does it!

VinceDee’s picture

I kind of figured by now (having been told so many times) that the best way to do the "why register" part would be to create a custom block. And I'll probably do that.

As far as "downloads" go: What do you mean by showing them a "static page"? This idea sounds interesting, but I'd like to know more.

Vince

---------
The hottest Linux distribution? Ubuntu
Talk about Ubuntu Web Servers at:
http://www.ubuntuwebservers.com

pigsie’s picture

Pakwaan

I'm interested in the modules you used to achieve this but the link you provided does not work unfortunately. Could you let me know what modules you used?

Thanks

Martin Haanz’s picture

"The 'easy' way to do tbis is to set up a separate menu with items for anonymous users. That menu will be its own bloick, Then show/hide tbat block based on roles.

(Since this can be done through site admin without any code, I call it easy. That's why the first poster always got this type of answer from people.)"

So how exactly do you show/hide blocks (based on roles) through site admin without any code?

Thanks.

bdanchilla’s picture

In the "pages" area of a block/node you click on '...PHP returns true..." and have something like

global $user;

return (
//admin user
 ($user->uid == 1) ||

((is_array($user->roles)) && 
(in_array("role1TItle", $user->roles)) || 
(in_array("role2TItle", $user->roles))) 
);

But, if you have a lot of roles, then you need many blocks to restrict access, and this is not a good way of doing things.
I'm looking for a way to restrict access on individual menu items myself...

iandickson’s picture

You could do it by creating custom php block.

Your echo statements for each menu item (to write the HTML) are set in side condition testing.

php - if (role is not anon){
echo ' html for link'
}

would hide from anon users.

Could work for you if your menus are pretty much fixed, but would be a pig to change with every menu edit.

Kodaboina Siddartha’s picture

I am new to drupal, Trying to display a block only if user has two specific roles. Just need more explanation, since IDK where I need to put that code

agentrickard’s picture

Note: Drupal 5 and higher.

In the block admin screen, click the 'configure' link next to the block.

Then look for the page section labeled: 'Role specific visibility settings'

Then follow the directions.

 Show block for specific roles:

  [] anonymous user
  [] authenticated user
     
Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.

--
http://ken.blufftontoday.com/
http://new.savannahnow.com/user/2
Search first, ask good questions later.

MooMaster’s picture

Sounds like a good suggestion, though it's not apparently obvious how to do it without code. I create an anonymous user navigation menu in my menus, then I create a new block...but how do I actually define the block's content to be the navigation menu? Embedding a SQL command to query the menu table sounds like code to me (and a brutally hackish way of doing it) and the only immediate options given for block content are HTML and PHP...

Assuming I have the menu created, when I'm defining the block how do I set its content to be the menu?

Moo Moo Moo

zenoyu’s picture

In case someone jump into same question, there is another module outside may help:

http://drupal.org/project/menu_per_role

(remember to follow the readme.txt to do the menu.inc patch)

garryp’s picture

Having to use a block just to control a menu item gets tiring, especially when otherwise there would be no block.

I thought menu-per-role would be my godsend for this issue, but alas, it does not work, even with the patch to menu.inc to hook into it. At least not on the PRIMARY menu, where I want to use it.

The basic module works ok with the db. It is the patch that links menu.inc to it that does not work.

But if anyone has succeeded in getting this module to work on the PRIMARY menu, I would very greaty appreciate a heads-up and a how-to!

.kuma’s picture

Thank you brian_d!!

I had to add this to make mine work:

...(in_array("role1TItle", array_values($user->roles))) || ...

o4tuna’s picture

I'm using Taxonomy Access Control to restrict access by user role in a Drupal5 installation. The Remove Nonviewable Menu Items module made hiding the off limits menu items easy.

http://drupal.org/project/remove_nonviewable_menu_items

ash211’s picture

Another vote for this module -- simple and effective.

gstokes’s picture

I have to say I find this unbelievable long winded, If I have a menu item amongest other menu items I have to create a new block menu just so I can have selective role access. Surely there is a more tidy way to to do this.

smptebars1’s picture

Awhile back I wanted a link on the main navigation menu that would only show up once logged in. In my case it was a support page link. I believe I did it using "views" and it worked. it even blocked the node (page) from being accessed directly if you were not signed in. But now I find myself wanting to remove the why register link from the main navigation for already logged in customers since they do not need to register again.... I am pulling my hair out because I can not remember how I did this before, but like I said... I think I used views. Anybody have any advice?

dvasquez’s picture

Hello, you can use this module: http://drupal.org/project/menu_access

I hope help you!