How to set Drupal group context

1k Views Asked by At

How do I set group context in Drupal 7?

I found this in og_context api:

**> 7 og_context.module og_context($group_type = 'node', $group = NULL)

Get or set group context using the menu system.

Parameters

$group_type: The context to get by group type. Defaults to "node".

$group: Optional; The group entity to set as the context.

Return value

Array keyed by the group type and group ID, or FALSE if no context was found.**

But I have not found any examples of how to enter "The group entity". I just know the group node ID I want to use (For example, "40").

Can anyone help me with this? Thanks!

2

There are 2 best solutions below

0
user1866032 On

I found the solution here: https://drupal.org/comment/8179187#comment-8179187

Assuming arg(1) is the group node id:

$node = node_load(arg(1));
og_context('node', $node); // Set og context
0
TechNikh On

This worked for me http://cgit.drupalcode.org/og_extras/tree/og_extras.module?h=7.x-1.x#n147

function mymodulename_og_context_negotiation_info() {
  $providers = array();
  $providers['mymodulename'] = array(
    'name' => t('mymodulename url'),
    'description' => t("Select group context for any url that starts with 'group/%'. Make sure that all views and custom pages use paths that start with this value in order for the context to be recognized when viewing those pages, and that nothing that is not a group uses that path."),
    'callback' => 'mymodulename_context_handler_url',
  );
  return $providers;
}

/**
 * Context handler; Get groups from URL.
 */
function mymodulename_context_handler_url() {
  $context = array();
  if (arg(0) == 'group' && is_numeric(arg(1))) {
    $context = array('node' => array(arg(1)));
  }
  return $context;
}