diff -u b/og_context/og_context.module b/og_context/og_context.module --- b/og_context/og_context.module +++ b/og_context/og_context.module @@ -391,12 +391,7 @@ */ 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, - 'file-ajax' => -3, - 'system-ajax' => -2, - ); + $defaults = array('url' => -5, 'node' => -4); if (!$enabled_providers = variable_get('og_context_negotiation_group_context', $defaults)) { return; } @@ -423,7 +418,12 @@ */ 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; } @@ -695,11 +695,6 @@ /** * Context handler; Shared between file upload and system ajax calls. - * - * This only works for _existing_ entities, not during entity creation. - * - * @todo: Extract the logic in og_context_handler_url() for detection of group - * using entityreference_prepopulate and use that here. */ function og_context_handler_ajax() { $item = menu_get_item(); @@ -715,7 +710,73 @@ } + + $gids = array(); $entity_type = $form['#entity_type']; - if (empty($form_state[$entity_type])) { - return; + 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]); } - return _group_context_handler_entity($entity_type, $form_state[$entity_type]); }