It's often helpful to set a group context via a view, a special page, or some menu item, so that the group's theme, blocks, page info, and other information can be loaded/displayed.

In Drupal 6, there was a specific function for this purpose: og_set_group_context() in og.module:

<?php
/**
* API function; Set the group context for the current request.
* Modules may set this as needed.
* This context is originally set during og_determine_context().
* @param $node
*   The group node object that should be set as the context.
*   You can use og_determine_context_get_group() to assist you with finding
*   the appropriate group node.
* @param $clear
*   Clear the group context.
* @return
*   The group node object if set.
*/
function og_set_group_context($node = NULL, $clear = FALSE) {
  static
$stored_group_node;
  if (
$clear) {
   
$stored_group_node = NULL;
  }
  if (!empty(
$node) && og_is_group_type($node->type)) {
   
$stored_group_node = $node;
  }
  return !empty(
$stored_group_node) ? $stored_group_node : NULL;
}
?>

Comments

Category:feature» support
Status:Active» Fixed

In D7 this functionality is now in og-context module. To get/ set a context you can call og_context().

Ah, didn't think of that. Is there a docs section on drupal.org where I could highlight a few of these things for others coming from D6 OG?

Also, is this the module you're speaking of? Context OG. If so, it seems it's not yet quite ready for D7: #1019386: Port to drupal 7.

No I'm talking about og_context -- it's part of the OG package.

Status:Fixed» Closed (fixed)

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

Status:Closed (fixed)» Active

re-opening for 2 reasons:

1. showing example of how i use og_context to set this for a view (in case people wanted to know)
- i have a view listing group content
- it has relationship for: OG membership: OG membership from Node
- i add a field: (OG membership from node) OG membership: Group ID (and set to not display)
- in view header i set php code as:

og_context('node', node_load($results[0]->og_membership_node_gid));

2. ask a question: where is the best place in code to assign default og_context() for other pages.
- i had following code in an _init hook:

  $group = og_context('node');
  if (!$group['gid']) {
    // first check if user is a member and if so; use that context
    if ($uid = $user->uid) {
      $userpage = user_load($uid);
      $group = node_load($userpage->og_user_node['und'][0]['target_id']);
      og_context('node', $group);
    }
    // failing that; set context as default site context
    else {
      $env = node_load(DEFAULT_GID);
      og_context('node', $env);
    }
  }

which basically checks if page already has og context. if it doesn't it sets default as the context for that user (based on their group membership) or sets a default site og context if the user does not have any membership.

this works great except it breaks what i do above for Views as setting the context in the view the way i do; is not picked up by the check in my _init hook.

hook_views_pre_view ?