og_session_context.module in OG Session Context 7
Provides hook implementations and functionality for og_session_context.
File
og_session_context.moduleView source
<?php
/**
* @file
* Provides hook implementations and functionality for og_session_context.
*/
/**
* Session variable to store space ID
*/
define('OG_SESSION_CONTEXT_ID', 'oa_space_id');
/**
* Implements hook_og_context.negotiation_info()
*
* Adds a method to /admin/config/group/context to grab the og context value
* through a custom session variable.
*/
function og_session_context_og_context_negotiation_info() {
$providers = array();
$providers['sessions'] = array(
'name' => t('Sessions'),
'description' => t("Determine context by checking session variable."),
'callback' => 'og_session_context_og_context_handler_sessions',
);
return $providers;
}
/**
* return the current group context
*/
function og_session_context_get_context() {
$item = menu_get_item();
$nid = NULL;
// Special support for /comment/reply/%.
if ($item['path'] == 'comment/reply/%') {
if (isset($item['original_map'][2]) && is_numeric($item['original_map'][2])) {
$nid = $item['original_map'][2];
}
}
elseif ($item['path'] == 'comment/%') {
if (isset($item['original_map'][1]) && is_numeric($item['original_map'][1])) {
if ($comment = comment_load($item['original_map'][1])) {
$nid = $comment->nid;
}
}
}
else {
$menu = menu_get_object();
if (isset($menu->nid)) {
$nid = $menu->nid;
}
}
if (!$nid) {
return 0;
}
$node = node_load($nid);
if (empty($node)) {
return 0;
}
if (og_is_group('node', $node)) {
// get space id directly from space nodes
return $nid;
}
elseif (!empty($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'])) {
// otherwise get space id from space field reference
return $node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'];
}
return 0;
}
/**
* Callback to pass the og context value based on the last space or space
* content page visited.
*/
function og_session_context_og_context_handler_sessions() {
$group = og_session_context_get_context();
// if we can't get the group from the node, look in session variable
if (!$group && !empty($_SESSION[OG_SESSION_CONTEXT_ID])) {
$group = $_SESSION[OG_SESSION_CONTEXT_ID];
}
og_session_context_set_context($group);
// update the session variable
$gids = FALSE;
// don't call node_load or check node access because this can be called
// early during batch processing
if ($group) {
$gids['node'][0] = $group;
}
return $gids;
}
/**
* Implements hook_exit().
*/
function og_session_context_exit($destination = NULL) {
// This can be called before Drupal sessions are bootstrapped
require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/session.inc');
// Skip call to context if we have no session.
global $user;
if ($user->uid == 0 && !drupal_session_started()) {
return;
}
// Ensure the latest og_context has been saved to the session.
$context = og_context('node');
if (!empty($context['gid'])) {
og_session_context_set_context($context['gid']);
}
}
/**
* Determines the og context value for og_session_context_og_context_handler_sessions
* by looking the entity's node value or the og_group_ref field value on
* space and space content page.
*/
function og_session_context_set_context($group) {
global $user;
// don't create a session for anonymous users unless session already started
// needed to allow Varnish to cache anonymous pages
if ($user->uid == 0 && !drupal_session_started()) {
return;
}
if ($group) {
$_SESSION[OG_SESSION_CONTEXT_ID] = $group;
}
}
Functions
Name | Description |
---|---|
og_session_context_exit | Implements hook_exit(). |
og_session_context_get_context | return the current group context |
og_session_context_og_context_handler_sessions | Callback to pass the og context value based on the last space or space content page visited. |
og_session_context_og_context_negotiation_info | Implements hook_og_context.negotiation_info() |
og_session_context_set_context | Determines the og context value for og_session_context_og_context_handler_sessions by looking the entity's node value or the og_group_ref field value on space and space content page. |
Constants
Name | Description |
---|---|
OG_SESSION_CONTEXT_ID | Session variable to store space ID |