Controlling who sees a block in Drupal

You can configure Drupal block visibility under Admin -> Blocks -> Configure. One option is "expert mode" whereby you can write a snippet of PHP code. If it returns true, the block is shown, otherwise it is hidden.

Some relevant code that works:

<?php
global $user;
return ((
in_array('writer', $user->roles))||($user->uid)==1);
?>

The global $user variable contains info on the current Drupal user.

in_array('writer', $user->roles)) checks to see if the current user is in the group 'writer' and returns true if so.

($user->uid)==1) returns true if the user's ID is 1. In Drupal, this means they are the admin user.

Therefore this block is shown to anyone who is either the admin or has the role of a "writer".


Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <del>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You may use [acidfree:xx] tags to display acidfree videos or images inline.
  • Images can be added to this post.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
14 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.