Apparently, this used to be possible with:

global $user;

if ($user->uid) {
if ($menu = menu_tree()) {
return $menu;
}
}

but not any more.

Also, if you don't know that, could you tell me in which .module the navigation block is defined?

Comments

droopy’s picture

global $user;

if ($user->uid) {
 if ($menu = theme_menu_tree()) {
   $menu = "<div class=\"menu\">". $menu . "</div>";
   return $menu;
 }
}
xand’s picture

Hmm. That works partially.

When authenticated, it works as usual.

when un-authenticated, the navigation block _title_ still appears, even though there is no content in the block.

---
www.symplification.com

droopy’s picture

Strange - it works properly on my site. Perhaps it's a theme issue - I use chameleon.

droopy’s picture

Strange - it works properly on my site. Perhaps it's a theme issue - I use chameleon.

xand’s picture

Strange. I'm using a derivation of "pushbutton", but I can't see how the theme would affect it. In fact, I suspect your code should probably be sufficient, because my addition should be implied.

I think that it doesn't really matter, the differences is really quite tiny.

---
www.symplification.com

xand’s picture

Interesting site.

I was browsing it a little, and http://www.opensourcetheology.net/node/228 generates a Fatal Error.

Also, I wonder if you would want a contributions such as:
http://www.symplification.com/downloads/evidence.pdf
Tell me if you would. (I assume you're Andrew)

Finally, I happen to use "droopy" as the username for drupal access to mysql. ;)

---
www.symplification.com

droopy’s picture

Thanks for spotting the fatal error. Why don't you contact me via OST and I'll respond on the other issue privately.

xand’s picture

Thanks! I've got it working:

global $user;
if ($user->uid) {
if ($menu = theme_menu_tree()) {
   $menu = "<div class=\"menu\">". $menu . "</div>";
   return $menu;
}
}
else {return;}

---
www.symplification.com

jbc’s picture

I want to do something similar. Can you explain how you actually use the php text in the blue box? Do I just cut and past the whole lot into the path box of the blocks admin page?

When I've attempted something similar in the past, it LOOKs as though only the bottom line has pasted...

(I'd also be interested to know how you "quote" php entry like that...)

thanks

xand’s picture

You create a new block under administer/block with my code block above as text.

To quote highlighted php, simply paste the php into your reply.

---
www.symplification.com

eloieli’s picture

My only problem with this nifty piece of code is that it no longer puts the persons username at the top of the Navigation menu.

Anyone know how to add this?

Steven’s picture

The module which handles the php coloring is codefilter, and can be found in the download section. It transforms any text between <?php and ?> tags into:

this;

stelman’s picture

I was wanting to do something like this!

stelman’s picture

One of the default features I like is the title of the block changing from "Navigation" to user_id after login. I looked through the code a little, but didn't find where this is done. Any idea how to add that to this block code?

chx’s picture

You need to write your own module, which implements at least hook_block() . Let test.module be something like this


function test_block($op = 'list', $delta = 0) {
  global $user;

  if ($op == 'list') {
     $blocks[0]['info'] = t('Test');
     return $blocks;
  }
  else {
    $block = array();

    switch ($delta) {
      case 0:
        if (!$user->uid) return;
        if ($menu = theme('menu_tree')) {
           $block['subject'] = $user->name;
           $block['content'] = '<div class="menu">'. $menu .'</div>';
        }
        return $block;
     }
  }
}

--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.

korayal’s picture

can we apply this code to the other menus we created from the "administer>menus"?

i mean;

global $user;
if ($user->uid) {
if ($menu = theme_menu_tree()) {
   $menu = "<div class=\"menu\">". $menu . "</div>";
   return $menu;
}
}
else {return;}

I guess in there we have to change the "theme_menu_tree()" but where do we find the code that we will change them with?

keve’s picture

theme_menu_tree($pid) function list the menu items with the parent menu item ($pid).

You can look up the parent menu id at /admin/menu
(when you click "edit", the $pid appears at the end of the url e.g: admin/menu/item/edit/42)
For example I have "Main Menu" defined with menu id #42.
So that i use the function theme_menu_tree(42) to list all the menu items under "main menu"

webengr’s picture

does this work for 4.6.2

and if so, where do you put it?

I GOT IT....... put a number in the menu tree function, 1 for the next menu and so on...

see my other thread-

http://drupal.org/node/27247

seannyob’s picture

groovy, good code, i modified it this way:

If you create a menu in administer > menus, and create a role 'foo' and only want your block to display to users in role 'foo', you can do this by creating a php block, finding the menu id (hover over the "edit" option, and get the trialing number) and using the following code where:

  • foo = your role
  • XX = your menu id number
<?php
global $user;
if (in_array('foo',$user->roles)) {
if ($menu = theme_menu_tree('XX')) {
$menu = "<div class=\"menu\">". $menu . "</div>";
}
}
return $menu;
?> 

--
Sean K. O'Brien
CTO
Colley Graphics, LLC
http://www.colleygraphics.com

markj’s picture

Sean, thanks a million, I tried this and it's perfect.

jbc’s picture

Could this php be changed quite simply to reflect the opposite requirement? I.e. say I have a "start here" block which I want unauthorised user to see. But once a user is authorised I want that block to disappear?

xand’s picture

Well, i'd imagine that the easiest way to do that would be to create another user login block, because that block automatically dissappears once logged in?

---
www.symplification.com

jbc’s picture

I don't understand how that would be done. Presumably the login block disappears because it's coded to within drupal somewhere. Just creating another block and calling it login isn't going to help, plus there's no option for replacing the old one. Am I missing something?

Say I did create another block using the php you've set out, can I still place html into it? for example to provide a select navigation list for the authorised user? Or would that have to be done in php?

droopy’s picture

This seems to work. You would need to use php to insert html.

global $user;

if (!$user->uid) {
  $output = "This box <b>disappears</b> once a user is logged in.";
	return $output;
	}				
jbc’s picture

Thanks, I'll look forward to getting this working: at my first attempt I'm getting the following error:

(I've had to insert "lesser-than angled bracket" in text, because otherwise the remainder of my post diappears. (how do I avoid that?)

Parse error: parse error, unexpected '[a lesser than bracket]' in /my-site.co.uk/public_html/modules/block.module(85) : eval()'d code on line 1

Drupal 4.4.2; I have php permission switched on and php selected within the block

Any ideas?

xand’s picture

You have to paste everything droopy wrote, including the < ? php and ? > tags?

---
www.symplification.com

jbc’s picture

everything was pasted and checked, just as per droopy's posting

Any other possiblilities?

xand’s picture

Try replacing $output with $content?

I know it works with Drupal 4.5...

---
www.symplification.com

xand’s picture

oh yes.

global $user;
if ($user->uid) { 
   $content = 'Hello World!'; 
   return $content;
}

Simply replacing "Hello World!" with whatever you please. It does not even have to be on one line, so you can simply paste whatever html code you had in mind.

(oops. I had this reply open for a long time :p)

You can use any variable name... the ! before $user determines whether you want the block to appear when there is a user, or when there isn't.
---
www.symplification.com

dmitrig01’s picture

make the code type for the block "PHP code", and inside:

global $user;
if ($user->uid) {
return;
}
else {

HTML goes here

}
_Martijn_’s picture

I'am trying to show the book navigation block for only one book. In total there are 4 books. I can't get it done with the path expressions. I also tried to alter the php code but this ended up with a completely disabled block.
If anyone can give me a pointer I would be grateful.

Martijn

xand’s picture

A possible method would be:

1. Enable path.module
2. edit *each* book page of that particular book to a certain path. e.g. about/1, about/2 etc.
3. set the path in the blocks module to that certain path. e.g. < ^ about >

This will mean that the book navigation module will only appear for that particular book.
---
www.symplification.com

dww’s picture

i wrote a patch to the 4.6 version of block.module that allows site admins to just select via checkboxes what roles should be able to view any given block. see http://drupal.org/node/49154 for details. also, this functionality will be added to 4.7 sometime soon (see http://drupal.org/node/18018 for info on that discussion).

___________________
3281d Consulting

nvl.sateesh’s picture

hi..

in order to display the Navigation Menu only for authenticated users, I edited the user.module like this:
1. Went to hook_block of the module.
2. it has 4 blocks: case 0, case 1, case 2 and case 3
3. in case 1, to the whole code i put the if clause as if($user->uid)

probably this is somewhere near the 583rd line of the module...

like this:

    case 1:
global $user;
if($user->uid){
if ($menu = theme('menu_tree')) {
           $block['subject'] = $user->uid ? check_plain($user->name) : t('Hi Guest');
           $block['content'] = $menu;
        }
}
        return $block;

it works well. i am not sure if editing the module has any issues. if there are any, plz correct me... i am newbie...

Walking the Path

Sateesh Nutulapati
Devops Solutions Architect at New Target, Inc.

Kresimir-1’s picture

I know it's an old thread, but i also wanted the navigation block to appear only for authenticated users and google brought me to this page.

So here is how it works (im using 6.8 version):

Home » Administer » Site building » Blocks - 'Navigation' block configure - Role specific visibility settings - Show block for specific roles: here select *only* authenticated user