I have about 6 different groups, eg Team A, Team B etc. I would like to display a block for one of these groups. Was thinking something like checking if the logged in users gid = X then display the block. That way I can setup 6 different blocks all targeting the different groups so that when the user is logged in, it will display the relevant block for them. Does that make sense? How would I implement it?
-
1Which version of drupal core do you use ?! – Muhammad Reda May 08 '12 at 14:37
-
sorry I should have mentioned that, need it for D7 – jamiez May 08 '12 at 23:06
2 Answers
Depending on your exact setup, it looks like the Context module may help you.
Here's how you can do that.
- Create your 6 separate blocks
- Download and install the context module
- Create a new context at admin/structure/context/add
- Fill in the Conditions section based on one of my options below
- Fill in the Reactions section, choose to add 'Blocks' and then select the exact block you want to show for the condition selected. You can show more than one, so add any that you want to appear.
- Create a separate context for each of your groups (so 6 in total). You can show multiple blocks per each group.
Creating a new context allows you to show certain blocks for only CERTAIN CONTEXTS. Example contexts are showing blocks on only certain pages (via the Path context) or only for users of a certain role (via the User role context) or even on certain node types or on pages that have a certain term attached, etc.
In your case, if you are using the Organic Groups module to implement your user groups, context will integrate with that. That means that when you create your context, there will be an option under the 'Conditions' section to select the Organic Group you want to show certain blocks for. You choose the exact blocks you want to show in the 'Reaction' section.
Let us know if that helps!

- 1,995
- 15
- 26
-
Hi Boriana, thanks for your response. I am aware of the context module and use if for the majority of my sites and am using it for this setup. Problem that I see with your solution is that context does not allow you to select individual groups that have been created, only the group type. I am needing to target each individual group within the one type. If I am wrong and infact it can do that, there maybe something wrong with my setup or I am missing something, and would be happy if you have any extra info for me that may help. thank you – jamiez May 10 '12 at 22:14
After more than a week of research and playing around, I found a little bit of code and modified it below to what I needed.
<?php
global $user;
$uid = $user->uid;
$result = db_query ( "SELECT * FROM {og_membership}
WHERE etid = :uid
and entity_type = 'user'
order by gid DESC", array (':uid' => $uid ) );
foreach ( $result as $row ) {
$gid = $row->gid;
break;
}
?>
<?php if ($gid == "GROUP ID HERE"): ?>
(load block here)
<?php endif; ?>

- 41
- 6