You are here

function og_context in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og_context/og_context.module \og_context()

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.

$account: Optional; The user entity we are getting context for. If empty, defaults to current user.

bool $check_access: Determines if access to the group should be done. Defaults to TRUE.

Return value

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

8 calls to og_context()
OgContextTestCase::testGettingGroupContext in og_context/og_context.test
Testing of the og_context returning value. Should return group id if user has permissions and NULL if he doesn't.
og_context_get_properties in og_context/og_context.module
Property getter callback.
og_context_init in og_context/og_context.module
Implement hook_init().
og_context_plugin_access_og_perm::access in og_context/includes/views/handlers/og_context_plugin_access_og_perm.inc
Determine if the current user has access or not.
og_context_plugin_argument_default_group_context::get_argument in og_context/includes/views/handlers/og_context_plugin_argument_default_group_context.inc
Return the group context argument.

... See full list

5 string references to 'og_context'
OgContextTestCase::setUp in og_context/og_context.test
Sets up a Drupal site for running functional and integration tests.
og_context_og_invalidate_cache in og_context/og_context.module
Implements hook_og_invalidate_cache().
og_context_views_api in og_context/og_context.module
Implements hook_views_api().
og_uninstall in ./og.install
Implements hook_uninstall().
og_user_roles_action_form in includes/actions/user_roles.action.inc

File

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

Code

function og_context($group_type = 'node', $group = NULL, $account = NULL, $check_access = TRUE) {
  global $user;

  // User account is sent.
  $account = $account ? $account : user_load($user->uid);
  $id = FALSE;
  if ($group) {
    list($id) = entity_extract_ids($group_type, $group);
  }
  $cache_keys = array(
    $group_type,
    $id,
    $account->uid,
    $check_access,
  );
  $cache_key = implode(':', $cache_keys);
  $context =& drupal_static(__FUNCTION__, array());
  if (!array_key_exists($cache_key, $context)) {

    // Mark that this context hasn't been checked yet.
    $context[$cache_key] = FALSE;
  }
  if (empty($group) && $context[$cache_key] !== FALSE) {

    // Set og_context_is_init to TRUE to define that function was already executed.
    og_context_is_init(TRUE);

    // Determine if we can return cached values.
    if (empty($context[$cache_key])) {

      // We already tried to find a context, but couldn't.
      return FALSE;
    }
    if ($context[$cache_key]['group_type'] == $group_type) {

      // Return the cached values.
      return $context[$cache_key];
    }
  }

  // Set the context to array, so we can know this function has been already
  // executed.
  $context[$cache_key] = array();
  if (!empty($group)) {

    // Set og_context_is_init to TRUE to define that function was already executed.
    og_context_is_init(TRUE);
    if (!og_is_group($group_type, $group)) {

      // The passed entity isn't a valid group.
      return FALSE;
    }
    if ($check_access && !entity_access('view', $group_type, $group, $account)) {

      // User doesn't have access to the group.
      return FALSE;
    }
    list($id) = entity_extract_ids($group_type, $group);
    $context[$cache_key] = array(
      'group_type' => $group_type,
      'gid' => $id,
    );
  }
  elseif ($gid = og_context_determine_context($group_type, NULL, $check_access)) {

    // Set og_context_is_init to TRUE to define that function was already executed.
    og_context_is_init(TRUE);
    $context[$cache_key] = array(
      'group_type' => $group_type,
      'gid' => $gid,
    );
    if ($user->uid) {

      // Save the group ID in the authenticated user's session.
      $_SESSION['og_context'] = array(
        'group_type' => $group_type,
        'gid' => $gid,
      );
    }
  }
  return $context[$cache_key];
}