diff --git a/docroot/sites/all/modules/contrib/og/og_context/og_context.module b/docroot/sites/all/modules/contrib/og/og_context/og_context.module index 3ea7d3f..c108f0c 100644 --- a/docroot/sites/all/modules/contrib/og/og_context/og_context.module +++ b/docroot/sites/all/modules/contrib/og/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; } @@ -333,7 +345,9 @@ function og_context_provider_weight($provider) { */ function og_context_determine_context($group_type, $item = NULL) { // Enable url and node context handlers by default. - $defaults = array('url' => -5, 'node' => -4); + //$defaults = array('url' => -5, 'node' => -4); + // Enable url, node, and file-upload context handlers by default. + $defaults = array('url' => -5, 'node' => -4, 'file-ajax' => -3); if (!$enabled_providers = variable_get('og_context_negotiation_group_context', $defaults)) { return; } @@ -605,3 +619,61 @@ function _og_context_views_page_access($group_type, $perm, $group_id_arg = FALSE return og_user_access($group_type, $group['gid'], $perm); } } + + /** + * Context handler; For file upload, get context from form's action + * (for new nodes) or from the group associated with the node being edited. + */ + function og_context_handler_ajax() { + + $item = menu_get_item(); + + $contexts = array(); + + if (strpos($item['path'], 'file/ajax') !== 0 && strpos($item['path'], 'system/ajax') !== 0) { + return; + } + + list($form, $form_state) = ajax_get_form(); + // Based on https://drupal.org/comment/6628310#comment-6628310 + $entity_type = $form['#entity_type']; + + if (empty($form_state[$entity_type])) { + return; + } + + $entity = $form_state[$entity_type]; + list($id) = entity_extract_ids($entity_type, $entity); + if (!module_exists('entityreference_prepopulate')) { + return; + } + $node_type = $form['#node']->type; + + if (!$fields = og_get_group_audience_fields('node', $node_type)) { + return; + } + + if (!$id) { + $gids = array(); + foreach ($fields as $field_name => $label) { + $field = field_info_field($field_name); + + $instance = field_info_instance('node', $field_name, $node_type); + $group_type = $field['settings']['target_type']; + // For file uploads when creating a new node, use the group based on the + // form's #action. + if (preg_match("/".$field_name."=(\d+)/", $form['#action'], $matches)) { + if (!isset($matches[1])) { + return; + } + $group_id = $matches[1]; + if (!og_is_group($group_type, $group_id)) { + return; + } + $contexts[$group_type][] = $group_id; + } + } + } + + return $contexts; +}