views:

223

answers:

2

I do not want the user login block to be displayed for users who are not admin. I only want it to be displayed for the admin user (sitadmin, uid:1, in my case) and users who are not logged in (uid:0)

In the configure page (/admin/build/block/configure/user/0) for the block, under page specific settings, I have selected "Show if the following PHP code returns TRUE (PHP-mode, experts only)." and given the following code:

<?php
global $user;

if($user->uid===1 or $user->uid===0)
  return TRUE;
else
  return FALSE;

?>

However, it is still visible even for users with uid 3,4 etc. I have tried clearing cache, with no avail. On the other hand, if I simply put

<?php return FALSE;?>

the user block is not shown for anonymous users.

Note: Role specific visibility settings don't seem to have an effect on this.

Any help?

A: 
<?php   
global $user;

if($user->uid==1 || $user->uid==0)
  return TRUE;
else
  return FALSE;
 ?>

would do the trick.

If you're unsure, always do a simple echo and display the block. For example:

<?php   
global $user;

if($user->uid==1)
  echo "Hello world!";
 ?>
SteD
+2  A: 

it also could be that in the $user array, uid is actually a string. so when you give it the === it tries to compare type as well. you could either do $user->uid==="1" or $user->uid==1

EDIT:

you could also try checking their user roles with

!in_array('authenticated user', array_values($user->roles)).

i think that's the logic you want. I'm not sure you're defining anything other than standard behavior for the login block... it only shows up if someone is NOT logged in? How is that different from normal?

contagious
well, if that's the case then I should always get FALSE irrespective of the user. But that's not the case! Anyways, I tried that; no luck.
LVS
My bad. :( I had imagined the 'user login' and 'navigation' block to be the same! I wanted the navigation visible only for admin users and not for others. So I move that code for 'navigation' block visibility, it works. Thanks!
LVS