Hi all!

I'm using Sections 6.x-1.x-dev together with Organic Groups (6.x-2.x-dev).
I'm trying with to switch theme if the user who is visiting the group node is also a member of the group.
(I can't just use OG theme functions because I need two different themes depending on the user status)

To do this I check if the user logged in has the current node among the groups he is member of.
It doesn't work - but the code I'm using works for blocks, so I'm a bit lost.

The following code adapted from http://drupal.org/node/201543#comment-661614 makes the block show up correctly if a user is member of the group:

<?php
global $user;
$nid = arg(1);
if(arg(0) == 'node' && is_numeric($nid)) {
  if (in_array($nid, $user->og_groups) {
    return TRUE;
  }
}
?>

This one adapted from http://drupal.org/node/201543#comment-1260103 does the trick as well:

global $user;
$nid = arg(1);
if(arg(0) == 'node' && is_numeric($nid)) {
  $account = user_load(array('uid' => $user->uid));
  if (isset($account->og_groups[$nid])) {
    return TRUE;
  }
}

I'm really no expert in PHP, but it seems that somehow Sections skips the in_array check, returning always TRUE: every node of any type gets the section theme, regardless of the user being a member of the group or not and even if the nodes are not group nodes.

Any suggestion? Alternative methods, places to look into...?

Thank you all!

Comments

smoothk’s picture

Category: support » bug

I thought I'd change the category to bug report since it looks like it's Sections that is not working properly. I'm still investigating the issue doing more tests but haven't come to anything new.

smoothk’s picture

Issue tags: +Organic Groups

It turned out that it was a problem with

$user->og_groups

which is an array of arrays, so the in_array() function doesn't work correctly.

I transformed it into a simple array with array_keys() and voilà! All good.

<?php
global $user;

// assures user is loaded properly
  if ($user->uid) {
    og_user('load', array(), $user);
  }
  else {
    $user->og_groups = array();
  }
    
  $node = $node = node_load(arg(1));

// transforms array of arrays into a simple array so it gets properly parsed
  $user_groups = array_keys($user->og_groups);
  
  if (in_array($node->nid, $user_groups)) {	  
    return TRUE;
  }  
?>
smoothk’s picture

Status: Active » Fixed

Forgot to change issue status. Done!

hass’s picture

Category: bug » support

Status: Fixed » Closed (fixed)
Issue tags: -Organic Groups

Automatically closed -- issue fixed for 2 weeks with no activity.