Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal - Display a block for a specific user group

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?

like image 905
jamiez Avatar asked Jan 23 '26 02:01

jamiez


2 Answers

Depending on your exact setup, it looks like the Context module may help you.

Here's how you can do that.

  1. Create your 6 separate blocks
  2. Download and install the context module
  3. Create a new context at admin/structure/context/add
  4. Fill in the Conditions section based on one of my options below
  5. 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.
  6. 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!

like image 153
Boriana Ditcheva Avatar answered Jan 24 '26 19:01

Boriana Ditcheva


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; ?>
like image 23
jamiez Avatar answered Jan 24 '26 19:01

jamiez