Created
May 21, 2015 07:26
-
-
Save amitaibu/a73e9d5caa983603ab74 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 c4m_og_access() { | |
if (!og_context_is_init()) { | |
// OG context wasn't called yet, so we can't call og_context() ourself yet. | |
return; | |
} | |
if (!$og_context = og_context()) { | |
// No OG context. | |
return; | |
} | |
} | |
/** | |
* Determines if OG context has been already called. | |
*/ | |
function og_context_is_init($called = FALSE) { | |
$return = &drupal_static(__FUNCTION__, FALSE); | |
if ($called) { | |
$return = $called; | |
} | |
return $return; | |
} | |
/** | |
* Get or set group context using the menu system. | |
* @param $group_type | |
* The context to get by group type. Defaults to "node". | |
* @param $group | |
* Optional; The group entity to set as the context. | |
* @param $account | |
* Optional; The user entity we are getting context for. If empty - defaults | |
* to current user. | |
* @param bool $check_access | |
* Determines if access to the group should be done. Defaults to TRUE. | |
* | |
* @return array | |
* Array keyed by the group type and group ID, or FALSE if no context | |
* was found. | |
*/ | |
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); | |
$context = &drupal_static(__FUNCTION__, FALSE); | |
if (empty($group) && $context !== FALSE) { | |
og_context_is_init(TRUE); | |
// Determine if we can return cached values. | |
if (empty($context)) { | |
// We already tried to find a context, but couldn't. | |
return FALSE; | |
} | |
if ($context['group_type'] == $group_type) { | |
// Return the cached values. | |
return $context; | |
} | |
} | |
// Set the context to array, so we can know this function has been already | |
// executed. | |
$context = array(); | |
if (!empty($group)) { | |
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 = array('group_type' => $group_type, 'gid' => $id); | |
} | |
// Get context from context handlers. | |
elseif ($gid = og_context_determine_context($group_type, NULL, $check_access)) { | |
og_context_is_init(TRUE); | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment