diff --git a/og_context/js/og_context.ajax.js b/og_context/js/og_context.ajax.js new file mode 100644 index 0000000..b252a60 --- /dev/null +++ b/og_context/js/og_context.ajax.js @@ -0,0 +1,39 @@ +(function ($) { + +/** + * Adds Organic Groups GET variables to jQuery Ajax request URLs. + */ +Drupal.behaviors.ogContext = { + attach: function (context, settings) { + if (Drupal.settings.ogContext) { + // The only reliable way to modify an Ajax request to add GET variables + // seems to be to override the jQuery.ajax() function entirely. See + // https://stackoverflow.com/questions/2332305/modifying-a-jquery-ajax-request-intercepted-via-ajaxsend + var jQueryAjax = $.ajax; + $.ajax = function (settings_or_url, settings) { + // jQuery allows this function to be called as either $.ajax(settings) + // or $.ajax(url, settings), so handle both cases here. + if (typeof settings_or_url === 'string') { + if (typeof settings === 'undefined') { + settings = {}; + } + settings.url = settings_or_url; + } + else { + settings = settings_or_url; + } + + // Modify the Ajax request URL to add the GET variable, but only if the + // request is being made to a URL on this site. + if (typeof settings.url !== 'undefined' && Drupal.urlIsLocal(settings.url)) { + settings.url += settings.url.indexOf('?') >= 0 ? '&' : '?'; + settings.url += 'og_ajax_context__gid=' + Drupal.settings.ogContext.gid + '&og_ajax_context__group_type=' + Drupal.settings.ogContext.groupType; + } + + jQueryAjax(settings); + }; + } + } +}; + +})(jQuery); diff --git a/og_context/og_context.admin.inc b/og_context/og_context.admin.inc index 027f94b..fe32098 100644 --- a/og_context/og_context.admin.inc +++ b/og_context/og_context.admin.inc @@ -44,8 +44,8 @@ function _og_context_configure_form_table(&$form) { ); $group_context_providers = $form['#group_context_providers']; - // Enable url and node context handlers by default. - $defaults = array('url' => -5, 'node' => -4); + // Enable URL, node, and Ajax context handlers by default. + $defaults = array('url' => -5, 'node' => -4, 'ajax-request' => -3); $enabled_providers = variable_get("og_context_negotiation_$type", $defaults); $providers_weight = variable_get("og_context_providers_weight_$type", $defaults); diff --git a/og_context/og_context.info b/og_context/og_context.info index 5e4e792..5416e90 100644 --- a/og_context/og_context.info +++ b/og_context/og_context.info @@ -3,6 +3,7 @@ description = "Get a group from a viewed page." package = "Organic groups" dependencies[] = og core = 7.x +scripts[] = js/og_context.ajax.js files[] = og_context.module files[] = og_context.install diff --git a/og_context/og_context.module b/og_context/og_context.module index 47df9b0..eb88be2 100644 --- a/og_context/og_context.module +++ b/og_context/og_context.module @@ -145,6 +145,12 @@ function og_context_og_context_negotiation_info() { 'menu path' => array('comment/reply/%', 'comment/%'), ); + $providers['ajax-request'] = array( + 'name' => t('URL (Ajax request)'), + 'description' => t('Determines context during Ajax requests by checking the group that was passed in via the URL by the page that made the Ajax request.'), + 'callback' => 'og_context_handler_ajax_request', + ); + return $providers; } @@ -405,8 +411,8 @@ function og_context_provider_weight($provider) { * The group ID for the current context, or FALSE if not found. */ function og_context_determine_context($group_type, $item = NULL, $check_access = TRUE) { - // Enable url and node context handlers by default. - $defaults = array('url' => -5, 'node' => -4); + // Enable URL, node, and Ajax context handlers by default. + $defaults = array('url' => -5, 'node' => -4, 'ajax-request' => -3); if (!$enabled_providers = variable_get('og_context_negotiation_group_context', $defaults)) { return; } @@ -651,6 +657,19 @@ function _group_context_handler_entity($entity_type = 'node', $entity = NULL, $p } /** + * Context handler; Get groups from the page that initiated an Ajax request. + */ +function og_context_handler_ajax_request() { + // Even though the requested group comes from untrusted URL parameters, it is + // not necessary to check that the user has access to the group in this + // function, since og_context_determine_context() does that before allowing + // the group to be used. + if (!empty($_GET['og_ajax_context__group_type']) && !empty($_GET['og_ajax_context__gid']) && og_is_group($_GET['og_ajax_context__group_type'], $_GET['og_ajax_context__gid'])) { + return array($_GET['og_ajax_context__group_type'] => array($_GET['og_ajax_context__gid'])); + } +} + +/** * Helper function to handle views page access. * * @param $group_type