You are here

function og_context_handler_node in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og_context/og_context.module \og_context_handler_node()

Context handler; Get groups from existing node or ctools context.

Parameters

$node: Optional; A node. If empty a node object will attempted to be loaded via menu_get_object().

1 call to og_context_handler_node()
og_context_handler_comment in og_context/og_context.module
Context handler; Get groups from parent of comment being added to it.
1 string reference to 'og_context_handler_node'
og_context_og_context_negotiation_info in og_context/og_context.module
Implements hook_og_context_negotiation_info().

File

og_context/og_context.module, line 528
Get a group from a viewed page.

Code

function og_context_handler_node($node = NULL) {
  if (empty($node) && ($node = menu_get_object())) {
    return _group_context_handler_entity('node', $node);
  }
  $item = menu_get_item();
  if (empty($item['map'])) {

    // User has no access to the menu item.
    return;
  }
  if (strpos($item['path'], 'node/%') === 0 && !empty($item['map'][1]) && !is_object($item['map'][1])) {
    $node = node_load($item['map'][1]);
  }
  elseif (strpos($item['path'], 'group/%/%/admin') === 0 && !empty($item['map'][1]) && $item['map'][1] == 'node') {
    $node = node_load($item['map'][2]);
  }
  if ($node) {
    return _group_context_handler_entity('node', $node);
  }

  // The path may not be %node, but in fact is a ctools-context, so extract the
  // node from it. We check only the 1st position (e.g. node/%/foo).
  if (empty($item['map'][1]) || !is_object($item['map'][1])) {
    return;
  }
  $context = clone $item['map'][1];

  // Check the context is a node type. We check only path similar to node/%/foo
  // and don't traverse over the whole arguments, as it might be a page manager
  // page passing multiple nodes (e.g. some/path/with/%node/%node). Implementing
  // modules wanting to handle the above example, should implement their own
  // context handler.
  if ($item['map'][1] instanceof ctools_context) {
    if (empty($context->type[0]) || $context->type[0] != 'entity:node') {
      return;
    }
    return _group_context_handler_entity('node', $context->data);
  }

  // We might have the node object as part of menu_get_item(), like in Webform
  // module nodes.
  // Check for vid and nid to make sure that this is a node, and not just an
  // entity that has an "nid" property.
  if (isset($context->vid) && isset($context->nid)) {
    return _group_context_handler_entity('node', $context);
  }
}