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:

/**
 * 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

amitaibu’s picture

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().

geerlingguy’s picture

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.

amitaibu’s picture

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.

liquidcms’s picture

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.

rv0’s picture

hook_views_pre_view ?

liquidcms’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Component: og.module » og-context
Issue summary: View changes

Many years later but will re-ask this again as possibly the functionality has changed moving to 7.x-2.x?

I have a View (page) where i set the og context as defined above. I added a block to the page which simply displays the GID taken from og_context().. just to make sure it is being set. And i see it is being set.

But, i have various OG blocks set which do not show up (I am pretty sure they used to back in 7.x-1.x days; but been a long time so can't swear to it). These blocks are standard OG blocks like "group details" and "og content" (latest og content). It would seem that at the time these blocks are checking for og context; the view has not had a chance yet to set it.

So, same question as back in 2013; is there a better way to set og context on a view (or panel) page such that other things (blocks, etc) which look for og context know these pages are part of the og?

liquidcms’s picture

This post helped: https://cryptic.zone/blog/defining-organic-groups-context-custom-pages

but mostly because it led me to this: /admin/config/group/context and told me enabling: "Group URL" and setting my View page to be at group/%/content