Is this possible? I have a javascript menu I would like all users to see but the navigation menu I want only visible to admins.

Comments

kje’s picture

I use following setup:

  1. Create a role "administrator" at /admin/access/roles
  2. Assign the role to your admin account at /user/1/edit
  3. Put following code (see below) in "pages" in your navigation block at admin/block/configure/user/1
  4. Select "[] Show if the following PHP code returns TRUE (PHP-mode, experts only)."
  5. You're done.
<?php 
global $user;
$roles = $user->roles;
if (in_array("administrator", $roles))
{return ('TRUE');}
?>

HTH, Kurt

erikajune’s picture

Thank you so much!

I followed all directions until this one where I'm stumped. There is not an option to select this. What do I do?

Select "[] Show if the following PHP code returns TRUE (PHP-mode, experts only)."

kje’s picture

Sorry for the delay. The option I meant is under:

administer > blocks

Click on "Navigation" configure

There you can paste the php snippet and activate the option
"[] Show if the following PHP code returns TRUE (PHP-mode, experts only)."

nevets’s picture

I am running 4.6.3 and do not see "Show if the following PHP code returns TRUE (PHP-mode, experts only)". When did it become available?

BinaryDigit’s picture

I'm trying to follow this and don't see "Show if the following PHP code returns TRUE (PHP-mode, experts only)". :( I'm running 4.6.3 as well
~Bit

kje’s picture

I'm running cvs (aka 4.7) Over the weekend I'll try to get 4.6.3 running to see what can be done.

erikajune’s picture

BTW, that code makes the entire left side disappear. LOL

zoinks’s picture

Kurt's code above got me started and I came up with a way to work it into 4.6.3. In /modules/user.module, look for "case 1:" and replace...

      case 1:
        if ($menu = theme('menu_tree')) {
           $block['subject'] = $user->uid ? $user->name : t('Navigation');
           $block['content'] = '<div class="menu">'. $menu .'</div>';
        }
        return $block;

...with...

	case 1:
	global $user;
	$roles = $user->roles;

	if ((in_array("UserRole1", $roles)) || (in_array("UserRole2", $roles)))
		{
		if ($menu = theme('menu_tree'))
			{
			$block['subject'] = $user->uid ? $user->name : t('Navigation');
			$block['content'] = '<div class="menu">'. $menu .'</div>';
			}
		return $block;
		}

You need to replace "UserRole1" and "UserRole2" with any user role(s) you've set up in your admin acess control section that you want to view the navigation menu. If you just have one user role that will have admin privileges, use "if (in_array("UserRole1", $roles))" instead.

Ah, open source... ;)

tclineks’s picture

zoinks, it's inadvisable to touch core when you don't have to.

This issue has been discussed in other threads, one example:
http://drupal.org/node/38427

zoinks’s picture

Thanks for the advice, tclineks. I tried your solution and it totally knocks out the navigation menu for any levels other than admin. Also tried separating out the elements I want other users to see... but couldn't add menu items into another block, or set user levels for new menus. So I'm sticking with my code mod above.

Speaking of my code mod above, it has an extra "{" and "}". It should be....

case 1:
global $user;
$roles = $user->roles;
if ((in_array("UserRole1", $roles)) || (in_array("UserRole2", $roles)))

if ($menu = theme('menu_tree'))
{
$block['subject'] = $user->uid ? $user->name : t('Navigation');
$block['content'] = '<div class="menu">'. $menu .'</div>';
}
return $block;

Otherwise it always shows the "Who's New" module. No idea why! (Yeah, yeah, that's why "it's inadvisable to touch core when you don't have to"...)

tclineks’s picture

Read more of the thread =)