function og_context_determine_context

7.x-2.x og_context.module og_context_determine_context($group_type, $item = NULL)
7.x-1.x og_context.module og_context_determine_context($item = NULL)

Determine the best matching context of a viewed page.

Parameters

$group_type: The entity type of the group. For example, "node" or "user".

$item: Optional; A menu item that context should be extracted from. If empty defaults to the current menu item by using menu_get_item().

Return value

The group ID for the current context.

1 call to og_context_determine_context()
og_context in og/og_context/og_context.module
Get or set group context using the menu system.

File

og/og_context/og_context.module, line 334
Get a group from a viewed page.

Code

function og_context_determine_context($group_type, $item = NULL) {
  // Enable url and node context handlers by default.
  $defaults = array(
    'url' => -5,
    'node' => -4,
  );
  if (!$enabled_providers = variable_get('og_context_negotiation_group_context', $defaults)) {
    return;
  }

  if (empty($item)) {
    $item = menu_get_item();
  }

  $providers = og_context_negotiation_info();
  foreach ($enabled_providers as $name => $ignore) {
    if (empty($providers[$name])) {
      continue;
    }
    $provider = $providers[$name];
    $invoke = FALSE;
    if (!empty($provider['menu path'])) {
      foreach ($provider['menu path'] as $path) {
        if (strpos($item['path'], $path) === 0) {
          $invoke = TRUE;
          // Path matches, so we can break.
          break;
        }
      }
    }
    else {
      // Context isn't determined by the menu item.
      $invoke = TRUE;
    }

    $gids = array();

    if ($invoke && ($contexts = call_user_func($provider['callback'])) && !empty($contexts[$group_type])) {
      // Check if one of the group IDs already exists in the session, and if
      // so use it.
      $gids = $contexts[$group_type];
      if (!empty($_SESSION['og_context']['group_type']) && $_SESSION['og_context']['group_type'] == $group_type && in_array($_SESSION['og_context']['gid'], $gids)) {
        $gid = $_SESSION['og_context']['gid'];
      }
      else {
        // Grab the first group ID.
        $gid = reset($gids);
      }
      // We found the first context, so we can break.
      break;
    }
  }

  return !empty($gid) ? $gid : FALSE;
}