2

I want a block to be visible only for Admin in Drupal 6.

To do it I've selected the third option(Show if the following PHP code returns TRUE (PHP-mode, experts only).) and entered the php code:

<?php
global $user;return $user->uid == 1;
?>

But the problem is that now this block shows only when I'm on some specific urls while logged in admin and not always when logged in admin. What's wrong?

I don't want to use the second option (Show on only the listed pages).

  • if($admin){} works in 7. check it may also work for 6. – Mujtaba Haider Sep 8 '11 at 6:59
  • Is $admin any global variable? If yes then doesn't it need global declaration? – user911216 Sep 8 '11 at 10:05
  • Are you sure you block don't have pages and/or content types restriction ? – silkAdmin Sep 8 '11 at 10:35
  • The block contains simple text: "hello world" – user911216 Sep 8 '11 at 13:37
  • might seem a bit trivial... but is the region in which your admin specific block assigned present in other page tpls also? – optimusprime619 Feb 6 '12 at 7:13
2

One more way to check for admin rights, sure it works in Drupal 6.

if (user_access('access administration pages')) {
    // do something
}
1

In this case, if the Block was created from a view (if not then you can create one) you can set the access settings in the view edit mode:

view access settingsgranular-settings

By selecting permissions you can make access to this block fine grained, based on rarely granted permissions such as 'administer-permissions' which is a permission only Administrators are likely to have. click on the little 'cog-wheel' to the right of the 'Access' row under the view's basic settings in order to select the restriction you desire: enter image description here

Please pay careful attention to the comment below the 'Default Access Options' list, as shown in the third image. It reads 'Only users with the selected permission flag will be able to access this display. Note that users with "access all views" can see any view, regardless of other permissions'.

You can remove the 'access all views' for the anonymous user under '/admin/user/permissions' and then set the permissions of your other views also. This gives more control over who sees what. Views by default have 'Access' set to 'Unrestricted' generally.

1

Sounds like an issue with your Drupal install. Is this a multisite or single? We have had issues with multisite inside of subfolders. For example: example.com/site1 example.com/site2 etc.

Cookies get all messed around when logged into one site and not the other. So it can make it seem like Drupal is behaving weird.

If this isn't your case you could always try assigning a role to Admin and then restricting the block that way instead of using PHP.

  • I'm observing the same. @jdwfly can you please tell me what other problems you faced with multisite installation? – AgA Feb 6 '12 at 5:43
0

$user->uid will return an error if $user is not an object.

Protect your code with try { } catch

  • 1
    Why would $user be not an object while being logged in as admin? After all $user is an global object and will always exist. – user911216 Sep 8 '11 at 10:06
0

this code works for me: (insert in "Show block on specific pages:")

<?php
global $user;
if(isset($user)) {
  if($user->uid == 1){
  return true;
  }
}
?>

i see this block in all browsed pages. in what pages/urls are missing the block?

  • But this code too works only when I'm at admin/build/block url. But for other url's the block does not show. I'm wondering... – user911216 Sep 9 '11 at 7:47
  • this is strange, seems that is a issue of your Drupal. If you try with your code (or other code) including it in some php node, does the problem still happens? Other alternative: review performance settings to see your cache settings. – cigotete Sep 9 '11 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.