diff --git a/og_context/og_context.module b/og_context/og_context.module index 0f76e39..89e6a7f 100644 --- a/og_context/og_context.module +++ b/og_context/og_context.module @@ -144,6 +144,18 @@ function og_context_og_context_negotiation_info() { 'callback' => 'og_context_handler_comment', 'menu path' => array('comment/reply/%', 'comment/%'), ); + $providers['file-ajax'] = array( + 'name' => t('File Ajax'), + 'description' => t("Determine context by checking the form's node's group or the form's action parameters during file uploads."), + 'callback' => 'og_context_handler_ajax', + 'menu path' => array('file/ajax'), + ); + $providers['system-ajax'] = array( + 'name' => t('System Ajax'), + 'description' => t("Determine context by checking the form's node's group or the form's action parameters during Ajax Call."), + 'callback' => 'og_context_handler_ajax', + 'menu path' => array('system/ajax'), + ); return $providers; } @@ -406,7 +418,12 @@ function og_context_provider_weight($provider) { */ 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); + $defaults = array( + 'url' => -5, + 'node' => -4, + 'file-ajax' => -3, + 'system-ajax' => -2, + ); if (!$enabled_providers = variable_get('og_context_negotiation_group_context', $defaults)) { return; } @@ -675,3 +692,91 @@ function _og_context_views_page_access($group_type, $perm, $group_id_arg = FALSE return og_user_access($group_type, $group['gid'], $perm); } } + +/** + * Context handler; Shared between file upload and system ajax calls. + */ +function og_context_handler_ajax() { + $item = menu_get_item(); + + if (strpos($item['path'], 'file/ajax') !== 0 && strpos($item['path'], 'system/ajax') !== 0) { + return; + } + + list($form, $form_state) = ajax_get_form(); + + if (empty($form['#entity_type'])) { + return; + } + + $gids = array(); + $entity_type = $form['#entity_type']; + if (!empty($form_state[$entity_type])) { + $gids = _group_context_handler_entity($entity_type, $form_state[$entity_type]); + if (empty($gids)) { + // _group_context_handler_entity() doesn't always return an array when + // empty reset to empty array. + $gids = array(); + + // @todo: Ideally we would not have to do this and we would just call og_context_handler_url(), + // however, it is not intelligent enough to handle other paths. + // Maybe we should add a new issue for that and abstact this logic + // out in a single place? + // @see og_context_handler_url(). + list($id, $vid, $bundle) = entity_extract_ids($entity_type, $form_state[$entity_type]); + $fields = og_get_group_audience_fields($entity_type, $bundle); + foreach ($fields as $field_name => $label) { + $field = field_info_field($field_name); + $instance = field_info_instance($entity_type, $field_name, $bundle); + $parsed_url = drupal_parse_url($form['#action']); + if (!empty($parsed_url['query']) && $ids = _og_context_entityreference_prepopulate_get_values_from_url_query($field, $instance, $parsed_url['query'])) { + // We need to validate those values ourself, as we called + // entityreference_prepopulate_get_values_from_url() directly, bypassing + // entityreference_prepopulate_get_values(). + $target_type = $field['settings']['target_type']; + $valid_ids = array(); + $entities = entity_load($target_type, $ids); + foreach ($entities as $id => $entity) { + if (!entity_access('view', $target_type, $entity)) { + // User can't access entity. + continue; + } + + if (!og_is_group($target_type, $entity)) { + // Entity is not a group. + continue; + } + + $valid_ids[] = $id; + } + + if ($valid_ids) { + $gids += array($target_type => array()); + $gids[$target_type] = array_merge($gids[$target_type], $valid_ids); + } + } + } + } + } + + return $gids; +} + +/** + * Retrieve the group values from the query paramaters for when we are creating + * content. Ideally we would just call entityreference_prepopulate_get_values_from_url(), + * however, we are unable to do so since it only cares about $_GET. + * + * @todo Add ticket and patach to entityreference_prepopulate to do this!? + * @see entityreference_prepopulate_get_values_from_url() + */ +function _og_context_entityreference_prepopulate_get_values_from_url_query($field, $instance, $query = array()) { + if (empty($query)) { + $query = $_GET; + } + + $field_name = $field['field_name']; + if (!empty($query[$field_name]) && is_string($query[$field_name])) { + return explode(',', $query[$field_name]); + } +}