Created
February 9, 2015 13:04
-
-
Save amitaibu/283277b53d2f6179c88a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (is_null($context[$cache_key])) { | |
// Mark that this context hasn't been checked yet. | |
$context[$cache_key] = FALSE; | |
} | |
if (empty($group) && $context[$cache_key] !== FALSE) { | |
// 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)) { | |
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 = array('group_type' => $group_type, 'gid' => $id); | |
} | |
// Get context from context handlers. | |
elseif ($gid = og_context_determine_context($group_type, NULL, $check_access)) { | |
$context = 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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment