Community

How to make the navigation block appear only for authenticated users?

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

Try this

<?php
global $user;

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

Hmm. That works partially.

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

A theme issue?

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

A theme issue?

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

Strange. I'm using a derivati

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

somewhat unrelated

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

Thanks

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

Thanks! I've got it working:

Thanks! I've got it working:

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

---
www.symplification.com

how to use php

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

Shalom!

Thank you for your attention,
john

communique.biz

You create a new block under

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

Block title

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?

codefilter.module

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:

<?php
this
;
?>

--
If you have a problem, please search before posting a question.

Thanks

I was wanting to do something like this!

block title = user_id ?

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?

You need to write a module

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

<?php
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.

this works great...

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

i mean;

<?php
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?

theme_menu_tree($pid)

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"

drupal 4.6,2 navigation only for authenticated users?

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

Display menu block by role

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

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

Sean, thanks a million, I

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

Block to disappear for authorised users

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?

Shalom!

Thank you for your attention,
john

communique.biz

Well, i'd imagine that the ea

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

how would that be done?

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?

Shalom!

Thank you for your attention,
john

communique.biz

This seems to do the trick

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

<?php
global $user;

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

parse error

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?

Shalom!

Thank you for your attention,
john

communique.biz

You have to paste everything

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

---
www.symplification.com

everything was pasted!

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

Any other possiblilities?

Shalom!

Thank you for your attention,
john

communique.biz

Try replacing $output with $c

Try replacing $output with $content?

I know it works with Drupal 4.5...

---
www.symplification.com

oh yes. Simply replac

oh yes.

<?php
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

make the code type for the

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

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

HTML goes here

<?php
}
?>

[Drupal++]

How to show block for only one book

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

A possible method would be:

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

patch to 4.6 block.module for this, real solution coming in 4.7

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).

__________________________________________________________________
My professional services are available through 3281d Consulting

i edited the user.module...

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:

<?php
   
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

I know it's an old thread,

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