I'd like to have some custom blocks which only show to particular user groups. Is there a way to do this? I'm using cvs version.

Comments

killes@www.drop.org’s picture

Sure. Just make you output depend on the permissions given to your user group (or 'role'). If there is no output returned, the block will not be displayed.

jasonwhat’s picture

Sorry, but I'm not sure where I control output. I'm pretty new at this. thanks.

killes@www.drop.org’s picture

If you write the code for a php based block, Drupal expects you to return the text for the block in a variable. If this variable is empty, then no block will be output.

jasonwhat’s picture

I'm not very good with php. what type of if/else statement would I use if I wanted a block to show only for say a group called "board?"
thanks

killes@www.drop.org’s picture

Put this inside the block definition (omit the php tags for Drupal 4.4.):

global $user;

$output = '';
if (in_array('board',$user->roles)) {

$output .= 'foo';
}

return $output;

Users with the board role will now see "foo" in the block.

Ian Ward’s picture

This works great, but I noticed an error that is created when blocks like this are turned on, and a user tries to login to the site, but uses an incorrect username and password...the error created is this:

warning: in_array(): Wrong datatype for second argument in /xxxxx/xxxxx/includes/common.inc(1822) : eval()'d code on line 4.

I searched a bit for errors of similar type in other modules but did not find much to try and solve it...any ideas?

The line in common is the drupal_eval function:

function drupal_eval($code) {
ob_start();
print eval('?>'. $code);
$output = ob_get_contents();
ob_end_clean();
return $output;
}

cheers, ian.

davidblewett’s picture

Hi killes:
I'm not sure if this is the "most Drupal" way to this, but it's the first idea I had. I want to include an off-site db query in a block to the side of the main content. I only want the block to show up after the user clicks a link. Is this possible?

killes@www.drop.org’s picture

Off-site db queries are a bit difficult. Search Drupal.org for more info.

Showing blocks after a user clicks a link might be possible by checking a referrer in the block function or some other tricks. There is also the adc theme that hides blocks with some JS.

pss0ft’s picture

Hi,

Just wondering. How to grant permission for blocks now? Can this be done? How to hide blocks when not logged in? With other words: hide for anonymous users? See my other posting about this.

I mean without hacking the code, but just fille in the right option. Possible already?

Grtz. Henk

jasonwhat’s picture

I've created a user dashboard using tables. I only want this block to show up for logged in users. I know how to do this if I'm just displaying some text, but how do I do it for a whole table?

Also, does anybody know the link I need so users can view "my blog" with the link recognizing the user?

Uwe Hermann’s picture

Some of your questions are answered here, I guess: http://drupal.org/node/13266

Uwe.
--
http://www.hermann-uwe.de